Input box filter non-digital JS code _javascript Tips

Source: Internet
Author: User

Html:

Copy Code code as follows:

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

Js:

Window.onload=function (e) {var Text=document.getelementbyid ("only"), Pattern=/\d/,//pattern match the number key on the letter pattern2=/(9[ 6-9]) | (10[0-5]) |3 (7|9)/,//pattern2 matches the numeric key and the left and RIGHT arrow keys on the keypad eventhandle={},event=e| | window.event;//an object that handles events//When a Web page loads, makes judgments, defines attributes for event-handling objects, so that the method of the event object needs to be judged only once, and no subsequent events//handlers need to judge if (Event.pre Ventdefault) {eventhandle.preventdefault=function (e) {E.preventdefault ();};} else{Eventhandle.preventdefault=function (e) {E.returnvalue=false}} text.onkeydown=function (e) {var event=e| | window.event;//event objects of different events, this event and the first event are not equal if (!pattern.test (String.fromCharCode (event.keycode)) && Event.keycode!=8//keycode=8 is the backspace key, because you can modify the number entered, so the BACKSPACE and LEFT ARROW keys do not prohibit &&!pattern2.test (event.keyCode.toString ()) ||
event.shiftkey| | event.ctrlkey| |
                               Event.metakey) {Eventhandle.preventdefault (event); If you do not use this method of the object, write the following, you can do it, but each time you press the keyboard, you will be judged. This is not necessary, so when the page load on the event-handling object to define a method, after loading the object of the method has been determined, and later on can be//if (event.preventdefAult) {//event.preventdefault ();
                                }else{//event.returnvalue=false;

 //}
}
}
}

Not commented 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);}}}

Inside the IE11, F12 Open the Developer tool to choose IE version to debug

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.