HTML:
The code copy is as follows:
<input type="text" id="only"/>
JS:
window.onload=function(e){var text=document.getElementById("only"),pattern=//d/,//pattern matches the number keys on the letter pattern2=/(9[6-9])|(10[0-5])|3(7|9)/,//pattern2 matches the number keys and left and right direction keys on the keyboard EventHandle={},event=e||window.event;//A object that handles events//When the web page is loaded, make a judgment and defines the attributes of the event processing object, so as to match the event The image method only needs to be judged once, and there is no need to judge in the subsequent events//handler 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;//The event objects of different events are different. This event is not equal to the first event if(!pattern.test(String.fromCharCode(event.keyCode))&&event.keyCode!=8//keyCode=8 is the backspace key. Because the input numbers can be modified, the backspace and left and right direction keys are not prohibited&&!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 it as follows, and you can also execute it. However, every time you press the keyboard, you will make a judgment. This is not necessary, so when the page is loaded, a method is defined for the event processing object. After loading, the method of the object is already determined. It will be fine in the future. //if(event.preventDefault){ //event.preventDefault(); //}else{ //event.returnValue=false; //}}}}}}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, if you open the developer tool in F12, you can select the IE version for debugging