i am using this code and working fine but the problem with this code is, its not optimized and for an example if i want to add few more characters then i will end-up having ifs….
if (keychar == "#" || keychar == "!" || keychar == "$" ..........)
is there a way to optimized this code? meaning less ifs (if i need to add more special characters to prevent)
<script language="javascript" type="text/javascript">
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event) {
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which) {
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "'" || keychar == "a") {
return false;
}
else {
return true;
}
}
</script>
OR