- 密码必须为8到16位且必须包含数字和字母
- 密码必须包含特殊字符【_&#%】
- 不能连续字符(如123、abc)连续3位或3位以上
- 不能相同字符(如111、aaa)连续3位或3位以上
const regFun = (str) => {// 密码必须包含数字和字母// 密码长度8到16位const reg1 = /(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}/if (!reg1.test(str)) {console.log('为弱口令密码')return false}// 密码必须包含特殊字符 _&#%if (!(str.indexOf("_") != -1 || str.indexOf("&") != -1 || str.indexOf("#") != -1 || str.indexOf("%") != -1)) {console.log('为弱口令密码')return false}// 不能连续字符(如123、abc)连续3位或3位以上if (!LxStr(str)) {console.log('为弱口令密码')return false}//不能相同字符(如111、aaa)连续3位或3位以上const reg2 = /(\w)*(\w)\2{2}(\w)*/gif (reg2.test(str)) {console.log('为弱口令密码')return false}}// 不能连续字符(如123、abc)连续3位或3位以上const LxStr = (str) => {let arr = str.split('')let flag = truefor (let i = 1; i < arr.length - 1; i++) {let firstIndex = arr[i - 1].charCodeAt()let secondIndex = arr[i].charCodeAt()let thirdIndex = arr[i + 1].charCodeAt()thirdIndex - secondIndex == 1secondIndex - firstIndex == 1if ((thirdIndex - secondIndex == 1) && (secondIndex - firstIndex == 1)) {console.log('为弱口令密码')flag = false}}}
// 四个符合三个(大写字母小写字母、数字、特殊字符、大于12位---固定条件)function pwReg (password) {const reg = /^((?![0-9]+$)|(?![`~!@#$%^&*-=\\+\|;:'",<.>/?]+$))(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9a-z]+$)(?!(.*)[\[\]](.*)+$)(?![A-Z0-9]+$)(?![0-9`~!@#$%^&*-=\\+\|;:'",<.>/?]+$)(?![a-zA-Z]+$)[a-zA-Z0-9`~!@#$%^&*-=\\+\|;:'",<.>/?]{12,}$/const result = reg.test(password)return result}// 判断密码是否为连续的数字或字母function lxStr (password) {let arr = password.split('');let flag = true;for (let i = 1; i < arr.length - 1; i++) {let firstIndex = arr[i - 1].charCodeAt();let secondIndex = arr[i].charCodeAt();let thirdIndex = arr[i + 1].charCodeAt();thirdIndex - secondIndex == 1;secondIndex - firstIndex == 1;if ((thirdIndex - secondIndex === 1) && (secondIndex - firstIndex === 1)) {flag = false;}}if (!flag) {return flag}return flag}// 判断是否连续相同数字或字母function lxSameStr (password) {const reg = /(\w)*(\w)\2{2}(\w)*/gif (reg.test(password)) {return false} else {return true}}