我遇到了密码验证问题。 那是我的代码:
function validatePassword(){
var password = document.getElementById("password").value;
var re = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
if(!password.match(re)){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");
return true;
}
它说它唯一无效,所以我认为它是因为正则表达式。 我问你是否可以在这里帮忙。
非常感谢帮助者!
答案 0 :(得分:4)
试试这个
// At least eight numbers or/and letters of English or Hebrew language
^[a-zA-Z0-9\u0590-\u05FF]{8,}$
或
// At least eight characters: one number, one uppercase, one lowercase English letter and one Hebrew letter
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[\u0590-\u05FF]).{8,}$
或者
// At least eight characters: one number and one uppercase of lowercase English or Hebrew letter
^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$
用法:
var p = /^(?=.*[0-9])(?=.*[a-z|A-Z|\u0590-\u05FF]).{8,}$/g;
var s = "שלוםWorld2";
if(!p.test(s)){
console.log("Invalid password!");
}
答案 1 :(得分:0)
也许你可以检查两次以简化它。 例如:
var password = document.getElementById("password").value;
var re1 = /^a-zA-Z0-9!@#$%^&*]{6,16}$/;
var re2 = /[0-9]/
var re3 = /[!@#$%^&*]/
if(password.match(re1) && password.search(re2) >=0 && password.search(re3) >=0){
producePromt("The password is invalid","commandPasswordPrompt","red");
return false;
}
producePromt("Password is OK","commandPasswordPrompt","green");