
April 26th, 2006, 04:17 AM
|
 |
Moderator From Beyond
|
|
Join Date: Sep 2004
Location: Israel
|
|
Javascript: deciding what characters can be written into textbox
The script below define list of valid characters that can be written
into the text box. any other character won't be written into the
text box when pressed. code has been tested for IE6 and FireFox.
javascipt section:
javascript Code:
Original
- javascript Code |
|
|
|
<script type="text/javascript"> var arrValidChars=new Array("a", "b", "c", "d", "e", "f", "g", "h", "i"); var arrSpecialChars = new Array(8, 9, 35, 36, 37, 38, 39, 40, 46); function VerifyCharacters(event, objInput) { var strText = objInput.value; var keyCode = event.keyCode||event.charCode; if (InArray(arrSpecialChars, keyCode) >= 0) return true; var strChar = String.fromCharCode(keyCode); return (InArray(arrValidChars, strChar)>=0); } function InArray(arr, key) { for (var i=0; i<arr.length; i++) { if (arr[i] == key) return i; } return -1; } </script>
html example:
html4strict Code:
Original
- html4strict Code |
|
|
|
<input type="text" onkeypress="return VerifyCharacters(event, this);" />
just change the variable arrValidChars to hold the valid letters
of your choice.
|