Limit text boxes to enter only numeric instance codes

Source: Internet
Author: User

The Qualified text box can only enter numeric instance code:
Sometimes the text box can be entered in the content only can be limited to numbers, such as postal code and telephone numbers. There are a number of ways to achieve this effect, such as regular expressions, but we don't use this, here's how to do this with the KeyCode property value.
The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Specifies that the text box can only enter numbers-ant tribe</title><Scripttype= "Text/javascript"> functionKP (EV) {varEV=EV||window.event; if(Ev.keycode<= -||Ev.keycode>= $) {Ev.returnvalue=false; } } functionKPD () {varEV=EV||window.event; if(Ev.keycode<= -||(Ev.keycode>= $&&Ev.keycode!= the) ) {Ev.returnvalue=false; }} window.onload=function(){  varOfirst=document.getElementById (" First"); varOsecond=document.getElementById ("Second"); Ofirst.onkeydown=KP; Osecond.onkeydown=KPD;}</Script> </Head> <Body>You can only enter 0-9:<inputtype= "text"ID= "First" /><BR/>only 0-9 can be entered.:<inputtype= "text"ID= "Second" /> </Body> </HTML>

The above code to achieve our needs, in the first text box can only enter 0-9 numbers, the second text box can only enter 0-9 and Point (.), the following describes the implementation process:
I. Principle of implementation:
Register the onkeydown event handler for two text boxes, which can be determined by the event object's KeyCode property, which is not valid if it is not within the specified range, or the character corresponding to the key will be entered in the text box.
Two. Related reading:
1.ev=ev| | Window.event can refer to var ev=window.event| | What is the role of EV as a chapter.
2.keyCode properties can be found in the KeyCode Properties section of JavaScript .
The 3.onkeydown event can be found in the OnKeyDown Event section of JavaScript .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9100

For more information, refer to: http://www.softwhy.com/javascript/

Limit text boxes to enter only numeric instance codes

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.