密码至少8位、包含数字、大小写字母、特殊字符。
"^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$"
密码8-20位、包含数字、大小写字母
var patrn1 = /^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,20}$/;
var patrn2 = /^(?![a-zA-Z]+$)(?![0-9]+$)[A-Za-z0-9]{8,20}$/;
1、前台验证密码
html:
<tr style="font: 12px/20px Arial;">
<td width="80" align="right">新密码:</td>
<td><input type="password" class="form-control" name="password" id="pwd2" onchange="checkNewPwd();"/> <span id="PWDcheckResult1" name="PWDcheckResult1" margin-left:5px; ></span></td>
</tr>
JS:
function checkNewPwd(){
var pwd = $("#pwd2").val().replace(/(^\s*)|(\s*$)/g, "");
if(pwd !=null && pwd != ""){
//长度大于8 且包含大小写字母、数字及特殊字符
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
if(strongRegex.test(pwd)){
$("#fLoginPWDImg").hide();
$("#PWDcheckResult1").css("color","green");
$("#PWDcheckResult1").html("√");
}else{
$("#pwd2").val("");
$("#PWDcheckResult1").css("color","red");
alert("密码长度至少8位,并且包含大小写字母、数字及特殊字符");
}
}
}
2、后台验证密码是否合格
Pattern pattern = Pattern.compile("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$");
Matcher matcher = pattern.matcher(user.getPassword());
if(!matcher.find()) { }else{ }