JS-Textbox-事件

剪切板监听-只能粘贴数字

        (function(){
            var textbox = document.getElementById("txtPhone");
            EventUtil.addHandler(textbox, "paste", function(event){
                event = EventUtil.getEvent(event);
                var text = EventUtil.getClipboardText(event);
                if (!/^\d*$/.test(text)){
                    EventUtil.preventDefault(event);
                }
            });
        })();

返回文本框中选择的内容

        (function(){
            function getSelectedText(textbox){
                if (typeof textbox.selectionStart == "number"){
                    return textbox.value.substring(textbox.selectionStart,
                            textbox.selectionEnd);
                } else if (document.selection){
                    return document.selection.createRange().text;
                }
            }
            EventUtil.addHandler(window, "load", function(event){
                var textbox = document.forms[0].elements[0];
                EventUtil.addHandler(textbox, "select", function(event){
                    alert(getSelectedText(textbox));
                });
                textbox.focus();
            });
        })();

只能输入数字 与 ctrl 的组合

        (function(){
            var textbox = document.getElementById("txtPhone");
            EventUtil.addHandler(textbox, "keypress", function(event){
                event = EventUtil.getEvent(event);
                var target = EventUtil.getTarget(event);
                var charCode = EventUtil.getCharCode(event);
                if (!/\d/.test(String.fromCharCode(charCode)) && charCode > 9 && !event.ctrlKey){
                    EventUtil.preventDefault(event);
                }
            });
        })();

选中文件框中的内容

        (function(){
            function selectText(textbox, startIndex, stopIndex){
                if (textbox.setSelectionRange){
                    textbox.setSelectionRange(startIndex, stopIndex);
                } else if (textbox.createTextRange){
                    var range = textbox.createTextRange();
                    range.collapse(true);
                    range.moveStart("character", startIndex);
                    range.moveEnd("character", stopIndex - startIndex);
                    range.select();
                }
                textbox.focus();
            }
            var btn = document.getElementById("select-btn");
            EventUtil.addHandler(btn, "click", function(event){
                var textbox = document.forms[0].elements[0];
                selectText(textbox, 4, 7);
            });
        })();

获取焦点时,自动选中所有内容

        EventUtil.addHandler(window, "load", function(event){
            var textbox = document.forms[0].elements[0];
            EventUtil.addHandler(textbox, "focus", function(event){
                event = EventUtil.getEvent(event);
                var target = EventUtil.getTarget(event);
                target.select();
            });
            textbox.focus();
        });

满足一定长度后,自动跳转到下一个文本框

        (function(){
            function tabForward(event){
                event = EventUtil.getEvent(event);
                var target = EventUtil.getTarget(event);
                if (target.value.length == target.maxLength){
                    var form = target.form;
                    for (var i=0, len=form.elements.length; i < len; i++) {
                        if (form.elements[i] == target) {
                            if (form.elements[i+1]){
                                form.elements[i+1].focus();
                            }
                            return;
                        }
                    }
                }
            }
            var textbox1 = document.getElementById("txtTel1"),
                textbox2 = document.getElementById("txtTel2"),
                textbox3 = document.getElementById("txtTel3");
            EventUtil.addHandler(textbox1, "keyup", tabForward);
            EventUtil.addHandler(textbox2, "keyup", tabForward);
            EventUtil.addHandler(textbox3, "keyup", tabForward);
        })();