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