繁体   English   中英

想要使用 js 允许 Backspace 或 spcae 按钮

[英]Want to allow Backspace or spcae button using js

我正在使用此脚本来限制所有特殊字符,但我只想允许 Backspace(8) 和 space(32) 键。

var specialKeys = new Array();
//specialKeys.push(8);  //Backspace
specialKeys.push(9);  //Tab
specialKeys.push(46); //Delete
specialKeys.push(36); //Home
specialKeys.push(35); //End
specialKeys.push(37); //Left
specialKeys.push(39); //Right

function IsAlphaNumeric(e) {
    var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
    var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
    document.getElementById("error").style.display = ret ? "none" : "inline";
    return ret;
}
<input name="pname" list="pnames" id="pname" type="text" class="form-control" autocomplete="off" onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;" required/>
<span id="error" style="color: Red; display: none">* Special characters are not allowed.</span>

请帮助我该怎么做..提前致谢

 function IsAlphaNumeric(e) { var k; document.all? k = e.keyCode: k = e.which; var ret = ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57)); document.getElementById("error").style.display = ret? "none": "inline"; return ret; }
 <input name="pname" list="pnames" id="pname" type="text" class="form-control" autocomplete="off" onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;" required/> <span id="error" style="color: Red; display: none">* Special characters are not allowed.</span>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM