[学习笔记]vue密码强度判断

老规矩,先上效果图:(方便演示,input中type改为了text)

 

判断规则用了这个https://blog.csdn.net/Ylxin/article/details/7725784

前面评分规则差不太多,后面评分等级作了修改:

小于10:密码强度不合格,10-25:弱,25-35:中,35以上:强

          <div class="passwordstrength">
            <div class="level1" :style="{'background-color':(score>=10&&score<25)?'#FC5F76':((score>=25&&score<35)?'#FF9900':(score>=35?'#33CC00':'#BBBBBB'))}"></div>
            <div class="level2" :style="{'background-color':((score>=25&&score<35)?'#FF9900':(score>=35?'#33CC00':'#BBBBBB'))}"></div>
            <div class="level3" :style="{'background-color':score>=35?'#33CC00':'#BBBBBB'}"></div>
          </div>
          <input type="password" placeholder="请输入您的密码" v-model="newpassword" maxlength="16" @input="passwordcomplex()" />

整体按照前面链接给出的判断规则来的,根据是否存在数字,是否存在大小写字母,是否存在特殊字符来进行打分,有连续的情况扣分,我还单独加了一个规则,如果整个密码都是连续的,分数直接扣为0。

接下来上js代码,函数返回的是最后的得分:

    passwordcomplex(){
      let passwordscore = 0
      let pwdArr = this.newpassword.split('');
      console.log(pwdArr);
      // pwdLen长度
      if(pwdArr.length>4&&pwdArr.length<=7){  //长度在4-7之间,加五分
        passwordscore += 5
      }else if(pwdArr.length>7){  //长度在7以上,加10分
        passwordscore += 10
      }
      // letter字母
      if(pwdArr.some(item => {return /^[a-z]$/.test(item)})){  //是否存在小写字母
        if(pwdArr.some(item => {return /^[A-Z]$/.test(item)})){  //是否存在大写字母
          passwordscore += 10   //大小写都有,加10,否则加5
        }else{
          passwordscore += 5
        }
      }else if(pwdArr.some(item => {return /^[A-Z]$/.test(item)})){
        passwordscore += 5
      }
      //num数字
      if(pwdArr.some(item => {return /^[0-9]$/.test(item)})){ //判断是否存在数字
        let count = 0
        pwdArr.forEach(item => {   //判断数字出现的次数
          if(/^[0-9]$/.test(item)){
            count++
          }
        })
        if(count>=3){  //出现大于等于3次,加10,否则加5
          passwordscore += 10;
        }else{
          passwordscore += 5;
        }
      }
      //special特殊字符
      if(pwdArr.some(item => {return /^[\^%&'*.,;=+\-?@#!$\x22]$/.test(item)})){ //判断是否存在特殊字符
        let count = 0
        pwdArr.forEach(item => {  //特殊字符出现次数
          if(/^[\^%&'*.,;=+\-?@#!$\x22]$/.test(item)){
            count++
          }
        })
        console.log('count',count);
        if(count>=2){
          passwordscore += 15;  //出现两次以上加15,否则加5
        }else{
          passwordscore += 5;
        }
      }
      //是否连续
      let isContinued = false;
      let countinuedCount = 0;
      for(let i =0;i<pwdArr.length;i++){
        if(pwdArr[i+1]){
          if((pwdArr[i].charCodeAt(0)+1==pwdArr[i+1].charCodeAt(0))||(pwdArr[i].charCodeAt(0)-1==pwdArr[i+1].charCodeAt(0))){  //如果相邻两个字符连续
            isContinued = true;  //开始记录连续
            countinuedCount++    //记录连续次数
          }else{
            if(isContinued){
              isContinued = false;
              passwordscore -= countinuedCount   //结束当前连续时,分数扣掉连续次数
              countinuedCount = 0
            }
          }
        }
      }
      console.log(isContinued,countinuedCount);
      if(countinuedCount == pwdArr.length-1){
        passwordscore = 0   //如果整个密码连续,分数为0
      }else{
        passwordscore -= countinuedCount
      }
      if(pwdArr.length<8){  //需求规定的,密码必须大于8位
        passwordscore = 0
      }
      for(let i = 0;i<pwdArr.length;i++){  //如果整个密码由单一字符构成,分数为0
        if(i ==pwdArr.length-1){
          passwordscore = 0
        }else if(pwdArr[i]!=pwdArr[i+1]){
          break
        }
      }
      console.log(passwordscore);
      this.score = passwordscore
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值