语言是javascript。
将通过的字符串:
不会通过的字符串:
我尝试了以下方法:
var matches = password.match(/\d+/g);
if(matches != null)
{
//password contains a number
//check to see if string contains a letter
if(password.match(/[a-z]/i))
{
//string contains a letter and a number
}
}
最佳答案
您可以使用正则表达式:
我从这里拿的:Regex for Password
var checkPassword = function(password){
return !!password.match(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #+=\(\)\^?&])[A-Za-z\d$@$!%* #+=\(\)\^?&]{3,}$/);
};
我使用这个正则表达式:
最少 3 个字符,至少 1 个字母、1 个数字和 1 个特殊字符:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #=+\(\)\^?&])[A-Za-z\d$@$!%* #=+\(\)\^?&]{3,}$"
此正则表达式将强制执行以下规则:
至少一个英文字母,(?=.*?[A-Za-z])
至少一位,(?=.*\d)
至少一个特殊字符,(?=.[$@$!% #+=()\^?&]) 如果您愿意,请添加更多...
3 个字符的最小长度 (?=.[$@$!% #?&])[A-Za-z\d$@$!%* #+=()\^?&]{3,} 包括空格
如果要添加更多特殊字符,可以将其添加到 Regex 中,就像我添加了 '(' (需要在两个地方添加)。
对于那些问自己这两个感叹号是什么的人,答案是:What is the !! (not not) operator in JavaScript?
关于javascript - 如何检查字符串是否至少包含一个既不是数字也不是字母的数字、字母和字符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40881969/