正则表达式验证 密码必须由大写字母、小写字母、数字、特殊符号中的3种及以上类型组成且不能出现连续的数字或者字母

291 阅读1分钟

拿到这个需求,首先要拆分一下

1.首先通过正则表达式验证一下密码必须由大写字母、小写字母、数字、特殊符号中的3种及以上类型

^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,}$

2.其次如果正则表达式通过,则验证一下字母或者数字是否连续

// 判断密码是否为连续的数字或字母
    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
    }