Here is an awesome script from htmlcodetutorial.com on how to restrict an input textbox to numbers only. View the source here.
<SCRIPT TYPE="text/javascript"> <!-- // copyright 1999 Idocs, Inc. http://www.idocs.com // Distribute this script freely but keep this notice in place function numbersonly(myfield, e, dec) { var key; var keychar; if (window.event) key = window.event.keyCode; else if (e) key = e.which; else return true; keychar = String.fromCharCode(key); // control keys if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) return true; // numbers else if ((("0123456789").indexOf(keychar) > -1)) return true; // decimal point jump else if (dec && (keychar == ".")) { myfield.form.elements[dec].focus(); return false; } else return false; } //--> </SCRIPT>
Now we can create a numbers only field using the attribute. For our first example, we'll create a field which accepts five digits as for a United States ZIP Code. Set the attribute exactly like it is here:
onKeyPress
onKeyPress
<FORM ACTION="../cgi-bin/mycgi.pl" METHOD=POST> U.S. ZIP Code: <INPUT NAME="dollar" SIZE=5 MAXLENGTH=5 onKeyPress="return numbersonly(this, event)"> <INPUT TYPE=SUBMIT VALUE="go"> </FORM>
0 comments:
Post a Comment