如何使用 JavaScript 只允许字母数字字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6256008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Only Allow Alpha Numeric Chars With JavaScript
提问by Jason
Been playing around with JavaScript, and what Im trying to do is only allow certain characters in the pass word field - a-z, A-Z and 0-9.
一直在玩 JavaScript,我试图做的是只允许密码字段中的某些字符 - az、AZ 和 0-9。
<form action="http://www.cknuckles.com/cgi/echo.cgi" method="get" name="logOn">
User Name:<br />
<input type="text" name="userName" size="25" /><br />
Password:<br />
<input type="password" name="pw" size="25" /><br />
<input type="submit" value="Log In" onClick="validate()"/>
</form>
Above is my HTML, and Below is my JavaScript I tried to use to validate it - but it doesnt work - any clues.
上面是我的 HTML,下面是我试图用来验证它的 JavaScript - 但它不起作用 - 任何线索。
<script language="javascript">
document.logOn.onsubmit=validate;
function validate(){
var name=document.logOn.pw.value;
if(!name = "[a-zA-Z0-9]"){
alert("Your Password Cant Have Any Funky Things In It - Play It Straight!");
return false;
}
return true;
}
</script>
But This isnt working. I can still put chars in like "*" and "[" and "{" etc.
但这不起作用。我仍然可以输入字符,如“*”和“[”和“{”等。
Any Thoughts?
有什么想法吗?
回答by Mark Kahn
You need to make your condition test a regexp, not a string:
你需要让你的条件测试一个正则表达式,而不是一个字符串:
if(!/^[a-zA-Z0-9]+$/.test(name)){ ...
meaning:
意义:
^
-- start of line[a-zA-Z0-9]+
-- one or more characters/numbers$
-- end of line
^
-- 行首[a-zA-Z0-9]+
-- 一个或多个字符/数字$
- 行结束
or you could search for the inverse of that, which is "any non-accepted character":
或者您可以搜索相反的内容,即“任何不接受的字符”:
if(/[^a-zA-Z0-9]/.test(name)){
回答by maerics
if (name.match(/[\W_]/)) { //...
Meaning if the "name" string has any character which is a non-alphanumeric or an underscore then execute the block. Note that we have to separately check for underscore (_
) because the alphanumeric character class (\w
) includes the underscore (so the negative class (\W
) does not).
意思是如果“名称”字符串有任何非字母数字或下划线的字符,则执行该块。请注意,我们必须单独检查下划线 ( _
),因为字母数字字符类 ( \w
) 包括下划线(因此负类 ( \W
)没有)。
回答by Srikrushna
The below solution will accept only the alpha numeric value and some special key like backspace, deleteetc.
If you want to add some other key then add to the array .
If you want to add some special symbol add to the same array or create a new array and compare it on the function like ex:
yourNewVariableName.indexOf(e.keyCode)
下面的解决方案将只接受字母数字值和一些特殊键,如退格、删除等。
如果您想添加一些其他键,请添加到数组中。
如果要添加一些特殊符号,请添加到同一个数组或创建一个新数组并在函数上进行比较,例如 ex:
yourNewVariableName.indexOf(e.keyCode)
var specialKeys = [8, 9, 46, 36, 35, 37, 39];
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));
return ret;
}
Alphanumeric value:
<input type="text" onkeypress="return IsAlphaNumeric(event);" ondrop="return false;" onpaste="return false;" />