Javascript Essentials Part 5


Characters Disallow – Restrict user from typing some characters

We can disallow some characters like sign of exclamation, at the rate of and others like estaric to type in text fields. Sometimes we do not want single or double quotes in input field. It is also good for security purpose. We can allow just characters or numbers or just alphabets in text field.

<html>
<BODY>
<center>
<script>
var isNS4 = (navigator.appName=="Netscape")?1:0;
</script>
<form onSubmit="return false;">
This field will not accept special characters: (like !@#$%^&* etc)<br>
<textarea rows=2 cols=20 name=comments onKeypress="if(!isNS4){if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;}else{if ((event.which > 32 && event.which < 48) || (event.which > 57 && event.which < 65) || (event.which > 90 && event.which < 97)) return false;}"></textarea>
<br><br>
This field will not accept double or single quotes:<br>
<input type=text name=txtEmail onKeypress="if(!isNS4){if (event.keyCode==34 || event.keyCode==39) event.returnValue = false;}else{if (event.which==34 || event.which==39) return false;}">
<br><br>
This field will only accept numbers:<br>
<input type=text name=txtPostalCode onKeypress="if(!isNS4){if(event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;}else{if(event.which < 45 || event.which > 57) returnfalse;}">
</form>
</center>
</body>
</html>

Related Posts


Leave a Reply

Your email address will not be published. Required fields are marked *