我正在使用这段代码并且工作正常,但这段代码的问题是,它没有优化,例如,如果我想添加更多字符,那么我最终会得到 ifs....
if (keychar == "#" || keychar == "!" || keychar == "$" ..........)
有没有办法优化这段代码?意味着更少的 ifs(如果我需要添加更多特殊字符来防止)
<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>