JS密码正则验证(不能连续字符(如123、abc)连续3位或3位以上)(不能相同字符(如111、aaa)连续3位或3位以上)

  • 密码必须为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
      }
    }

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值