它无法正常工作,我得到密码不匹配!对于我用字母,数字和大写字母写的每个密码
var reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"/;
var pass = document.getElementById("password").value;
if (pass == "") {
alert("Enter the user password");
document.getElementById("password").focus();
return false;
}
if (pass.length < 8) {
alert("Password too short, at least 8 characters");
document.getElementById("password").focus();
return false;
}
var pasw = pass.match(reg);
if (pasw) {
alert("Password is valid");
} else {
alert("Password doesn't match!");
return false;
}
答案 0 :(得分:0)
首先,根据此结果使用clearTimeout(timeout);
timeout = setTimeout(function(){...}, 2000);
代替var reg = ^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}$
:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
其次,使用var reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"/;
代替reg.test(pass)
。
test()方法测试字符串中的匹配项。 如果找到匹配项,则此方法返回true,否则返回 假的。
pass.match(reg)