要求:
正则表达式:密码中至少包含大写字母、小写字母、数字、特殊字符等字符中的2种或3种
解析:
经过同事提醒,换一种新的实现思路,按照正常理解要写一个很复杂的正则,无奈水平有限,遂采用“包含累加法”,解决密码复杂度校验问题,这里记录一下:
示例代码:
var pwdCount = 0;//统计密码包含了几种字符
var pwdRegex1 = new RegExp('(?=.*[0-9]).{8,30}');//纯数字
var pwdRegex2 = new RegExp('(?=.*[a-z]).{8,30}');//小写字母
var pwdRegex3 = new RegExp('(?=.*[A-Z]).{8,30}');//大写字母
var pwdRegex4 = new RegExp('(?=.*[^a-zA-Z0-9]).{8,30}');//特殊字符
if (pwdRegex1.test(newPassword)) {
pwdCount = pwdCount + 1;
}
if (pwdRegex2.test(newPassword)) {
pwdCount = pwdCount + 1;
}
if (pwdRegex3.test(newPassword)) {
pwdCount = pwdCount + 1;
}
if (pwdRegex4.test(newPassword)) {
pwdCount = pwdCount + 1;
}
if (pwdCount < 3) {
toastr.error("密码至少包含大写字母、小写字母、数字、特殊符号中的3种,且长度至少为8位!");
return;
}