项目中有个需要验证密码强度,我对正则表达式并不熟悉,每次都是上网google,这次照例,发现了一个,就用了,结果被QA测试出只能在safari上用,IE和FF不行,奇怪了。
function validatePassword_delete(password,error) {
if (password.value.length < {
error.innerHTML = "Password must be minimum 8 alphanumeric with complexity e.g. P4343g";
password.focus();
return false;
} else {
alert(password.value.replace(/^(?:([a-z])|([A-Z])| ([0-9])|(.)){5,}|(.)+$/g, "$1$2$3$4$5").length);
if(password.value.replace(/^(?:([a-z])|([A-Z])|([0 -9])|(.)){5,}|(.)+$/g, "$1$2$3$4$5").length<2){
error.innerHTML = "Password must be minimum 8 alphanumeric with complexity e.g. P4343g";
password.focus();
return false;
}else{
error.innerHTML = "";
return true;
}
}
}
于是我又google,终于找到一个万能的,贴上来吧
function validatePassword(pwd,error) {
var strength = document.getElementById('strength');
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9] )(?=.*\W).*$", "g");
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.* [A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{8,}).*", "g");
if (false == enoughRegex.test(pwd.value)) {
error.innerHTML = "Password must be minimum 8 alphanumeric with complexity e.g. P4343g";
pwd.focus();
return false;
} else if (strongRegex.test(pwd.value)) {
error.innerHTML ="";
return true;
} else if (mediumRegex.test(pwd.value)) {
error.innerHTML ="";
return true;
} else {
error.innerHTML = "Password must be minimum 8 alphanumeric with complexity e.g. P4343g";
pwd.focus();
return false;
}
}