[英]javaScript check for at least one alphanumeric character in string
In my application someone can submit text from another language. 在我的应用程序中,有人可以用其他语言提交文本。 I'd like for only English alphanumeric strings to be entered.
我只想输入英文字母数字字符串。 I've got this JavaScript function working but wondering if this is the best method for doing this?
我已经使此JavaScript函数正常工作,但想知道这是否是执行此操作的最佳方法吗?
var string = $('input[name=title]').val();
if((/\d/.test(string) || /[a-zA-Z]/.test(string)) === false) {
alert('Field has no Alphanumeric characters.');
fPass = false;
}
Even if some just enters 1 character I want that to be allowed, as long as it is a number or a character from one of the 26 letters in the English alphabet (case insensitive) 即使有人只输入1个字符,我也希望它被允许,只要它是数字或英语字母表中26个字母之一中的一个字符(不区分大小写)
Without using a function here is what I've come up with 在不使用功能的情况下,我想到了
if((/[\da-z]/i.test(string)) === false) {
alert('Please use alphanumeric characters.')
}
You can combine your regex into one expression. 您可以将正则表达式合并为一个表达式。
function hasAlphanumeric(str) {
return /\d|[A-z]/.test(str)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.