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