- 密码必须为8到16位且必须包含数字和字母
- 密码必须包含特殊字符【_&#%】
- 不能连续字符(如123、abc)连续3位或3位以上
- 不能相同字符(如111、aaa)连续3位或3位以上
// 取出密码的数字与字母
function getLetter (val) {
const reg1 = /(0\d{3,})|([1-9]\d+)/g
const reg2 = /[a-z]{3,}/g
const num = val.match(reg1)
const letter = val.match(reg2)
console.log('数字', num)
console.log('字母', letter)
const data = {
letter,
num
}
return data
}
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)*/g
if (reg2.test(str)) {
console.log('为弱口令密码')
return false
}
}
// 不能连续字符(如123、abc)连续3位或3位以上
const LxStr = (str) => {
let arr = str.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)) {
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)*/g
if (reg.test(password)) {
return false
} else {
return true
}
}