Input box to filter non-numeric javascript code _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces js characters used to filter non-numeric characters in the input box. It is very simple but practical. For more information, see HTML:

The Code is as follows:



JS:

Window. onload = function (e) {var text = document. getElementById ("only"), pattern =/\ d/, // pattern matches the alphanumeric key pattern2 =/(9 [6-9]) | (10 [0-5]) | 3 (7 | 9) // pattern2 matches the numeric key on the keypad and the left and right direction keys EventHandle = {}, event = e | window. event; // an event processing object // when a webpage is loaded, judge and define properties for the event processing object. In this way, the event object method only needs to be judged once, other events in the future // The if (event. preventDefault) {EventHandle. preventDefault = function (e) {e. preventDefault () ;};} else {EventHandle. preventDefault = fu Nction (e) {e. returnValue = false;} text. onkeydown = function (e) {var event = e | window. event; // The event objects of different events are different. The event is not the same as the previous event. 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 direction keys are not allowed &&! Pattern2.test (event. keyCode. toString () | event. shiftKey | event. ctrlKey | event. metaKey) {EventHandle. preventDefault (event); // if this method of the object is not used, it can be executed as follows. However, it is unnecessary to make a judgment every time you press the keyboard, therefore, when the page is loaded, define a method for the event processing object. After loading, the object method is determined and will be ready later. // 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, F12 can open the developer tool and select the IE version for debugging.

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.