我想在文本字段上检查按键或键盘事件上的特殊字符验证,该字段仅允许空格,A-Z,a-z,0-9。
任何想法 感谢
答案 0 :(得分:2)
您可以在this jsfiddle中找到此功能的演示:
function CommonKeyPressIsAlpha(e){
e = e || event;
var matched = (/[a-z]/i).test (String.fromCharCode(e.keyCode || e.which))
document.getElementById('report').innerHTML =
matched ? "" : "Valid input: 'a-z' and/or 'A-Z'"
return matched;
}
要允许数字,请将(/[a-z]/i).test
更改为(/[a-z0-9]/i).test
答案 1 :(得分:0)
这是关于这个问题的非常好的写作: http://www.htmlcodetutorial.com/forms/index_famsupp_160.html
答案 2 :(得分:0)
以下功能代码段将帮助您实现此目标
Function AlphaNumericValidation(){
var oneChar = "";
for(var i = 0; i < StringSample.length; i++) {
oneChar = StringSample.substring(i, i+1);
if ( (oneChar < "0") || (oneChar > "9" && oneChar < "A") || (oneChar > "Z" && oneChar < "a" ) || (oneChar > "z") )
{
if (isAcceptedChar(oneChar) == false)
{
return false;
}
}
}
return true;
}