Filter non-numbers in the input box

Source: Internet
Author: User

HTML: <input type = "text" id = "only"/>

JS:

 
1 window. onload = function (e) {2 var text = document. getelementbyid ("only"), pattern =/\ D/, // pattern matches the number key 3 pattern2 =/(9 [6-9]) on a letter. | (10 [0-5]) | 3 (7 | 9) // pattern2 matches the numeric key on the keypad and the left and right direction keys 4 eventhandle = {}, event = E | window. event; // an event processing object
// When a webpage is loaded, it determines and defines the attributes of the event processing object. In this way, the method of the event object only needs to be judged once. Other events in the future
// No judgment is required in the processing program
5 If (event. preventdefault) {6 eventhandle. preventdefault = function (e) {7 E. preventdefault (); 8}; 9} else {10 eventhandle. preventdefault = function (e) {11 E. returnvalue = false; 12} 13} 14 text. onkeydown = function (e) {15 var event = E | window. event; // The Event objects of different events are different. The event is not equal to the first event. 16 if (! Pattern. Test (string. fromcharcode (event. keycode) & event. keycode! = 8 // keycode = 8 is the return key. Because you can modify the entered number, the Return key and the left and right arrow keys do not prohibit 17 &&! Pattern2.test (event. keycode. tostring () | event. shiftkey | 18 event. ctrlkey | event. metakey) {19 eventhandle. preventdefault (event); // if this method of the object is not used, it can be executed as follows, but it will be judged every time you press the keyboard.
// This is unnecessary. Therefore, when loading a page, define a method for the event processing object. After loading the object, the method is fixed and will be ready later.

// If (event. preventdefault ){
// Event. preventdefault ();
//} Else {
// Event. returnvalue = false;
//}

20             }21         }22     }

Uncommented version:

window.onload=function(e){        var text=document.getElementById("only"),pattern=/\d/,                pattern2=/(9[6-9])|(10[0-5])|3(7|9)/,                EventHandle={},event=e||window.event;        if(event.preventDefault){            EventHandle.preventDefault=function(e){                e.preventDefault();            };        }else{            EventHandle.preventDefault=function(e){                e.returnValue=false;            }        }        text.onkeydown=function(e){            var event=e||window.event;            if(!pattern.test(String.fromCharCode(event.keyCode))&&event.keyCode!=8                    &&!pattern2.test(event.keyCode.toString())||event.shiftKey||                    event.ctrlKey||event.metaKey){                EventHandle.preventDefault(event);            }        }    }

In ie11, F12 can open the developer tool and select the IE version for debugging.

Filter non-numbers in the input box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.