I have a textbox in a HTML Form where I want that users should only enter dates. That's why I want that users should only be allowed to enter numbers from 0-9 & the '/' character in the textbox. This is how I am doing using the onKeyDown event:
<script language="JavaScript">
function handler(e) {
var key=e.keyCode;
if(key==13 || key==46 || key==191 || (key>47 && key<58))
return true;
else
return false;
}
</script>
<form>
<input type="text" onKeyDown="return handler(event)">
</form>
The above code will ensure that even if a user tries to type some letters (a, b, c, d.......), it won't be reflected in the textbox. But the above code does not take care of the following characters - !, @, #, $, %, ^, &, *, ( & ) i.e. the characters that are typed using the 'Shift' key + any number key (for e.g. Shift + 1 will type !, Shift + 2 will type @ etc...). How do I ensure that the users are not allowed to enter these special characters that are usually typed by pressing 'Shift' + any number key?
Thanks,
Arpan
<script language="JavaScript">
function handler(e) {
var key=e.keyCode;
if(key==13 || key==46 || key==191 || (key>47 && key<58))
return true;
else
return false;
}
</script>
<form>
<input type="text" onKeyDown="return handler(event)">
</form>
The above code will ensure that even if a user tries to type some letters (a, b, c, d.......), it won't be reflected in the textbox. But the above code does not take care of the following characters - !, @, #, $, %, ^, &, *, ( & ) i.e. the characters that are typed using the 'Shift' key + any number key (for e.g. Shift + 1 will type !, Shift + 2 will type @ etc...). How do I ensure that the users are not allowed to enter these special characters that are usually typed by pressing 'Shift' + any number key?
Thanks,
Arpan