本文实例为大家分享了js实现密码输入强度验证的具体代码,供大家参考,具体内容如下
需求:
1.需要对用户输入的密码进行验证,验证的级别分为强中弱,如果输入的密码强度少于6时,则不会验证,只有密码强度在6-20时才会进行验证。
相关的正则
//密码为八位及以上并且字母数字特殊字符三项都包括
    var strongRegex = new RegExp("^(", "g");
    //密码为七位及以上并且字母、数字、特殊字符三项中有两项,强度是中等
    var mediumRegex = new RegExp("^(", "g");
    var enoughRegex = new RegExp("(", "g");
2.密码的显示与隐藏,点击小眼可以对密码进行显示或者隐藏。
具体代码
html部分:
<div class="wrapper"> <div class="input_box"> <input type="password" name="" placeholder="请输入密码" oninput="passValidate(this)" id="inputPwd" value=""> <div class="eye_icon"></div> </div> <p>请至少使用字母、数字、符号两种类型组合的密码,长度为6~20位。</p> <ul class="pwdStrength"> <li class="weak"></li> <li class="middle"></li> <li class="strong"></li> <li class="result"></li> </ul> </div>
css部分:
 * {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
  }
  .wrapper {
   width: 500px;
   height: 200px;
   border: 1px solid #eee;
   margin: 100px auto;
   display: flex;
   align-items: center;
   flex-direction: column;
  }
  .input_box{
   width: 80%;
   display: flex;
   align-items: center;
  }
  .input_box input {
   width: 82%;
   height: 30px;
   border: none;
   outline: none;
   border: 1px solid #D2B48C;
   border-radius: 12px;
   margin: 10px 0px;
   padding-left: 15px;
  }
  .eye_icon{
   width: 20px;
   height: 20px;
   background-image: url('./open_eye.png');
   background-repeat: no-repeat;
   background-position: center content;
   background-size: cover;
   margin-left: 10px;
  }
  .wrapper p {
   width: 80%;
   height: 60px;
   line-height: 26px;
   font-size: 14px;
   color: #339999;
  }
  .pwdStrength {
   width: 80%;
   list-style: none;
   height: 30px;
   display: none;
   flex: 1;
  }
  .weak,
  .middle,
  .strong {
   height: 15px;
   width: 30px;
   border: 1px solid black;
   background: rgb(238, 238, 238);
  }
  .middle {
   border-left: 0;
   border-right: 0;
  }
  .result {
   width: 30px;
   height: 15px;
   font-size: 14px;
   line-height: 14px;
   text-align: center;
   margin-left: 10px;
  }
JS部分:
//密码的可见与隐藏、
  console.log($('#inputPwd'))
  var eyeFlag = false;
  $('.eye_icon').click(function(){
   if(!eyeFlag){
    $(this).css({'background-image': 'url(' + "./close_eye.png" + ')'});
    $('#inputPwd').attr('type','text');
   }else{
    $(this).css({'background-image': 'url(' + "./open_eye.png" + ')'});
    $('#inputPwd').attr('type','password')
   }
   eyeFlag = !eyeFlag;
  })
  //密码强度验证
  function passValidate(e) {
   var pwd = $.trim(e.value);
   if (pwd === '') {
    $('.pwdStrength').css({'display':'none'})
    $('.weak').css({
     'background': 'rgb(238, 238, 238)'
    });
    $('.middle').css({
     'background': 'rgb(238, 238, 238)'
    });
    $('.strong').css({
     'background': 'rgb(238, 238, 238)'
    });
    $('.result').text('')
   } else {
    $('.pwdStrength').css({'display':'flex'})
    //密码为八位及以上并且字母数字特殊字符三项都包括
    var strongRegex = new RegExp("^(", "g");
    //密码为七位及以上并且字母、数字、特殊字符三项中有两项,强度是中等
    var mediumRegex = new RegExp("^(", "g");
    var enoughRegex = new RegExp("(", "g");
    if (false == enoughRegex.test(pwd)) {
    } else if (strongRegex.test(pwd)) {
     $('.strong').css({
      'background': '#33ff33'
     });
     $('.result').text('强')
    } else if (mediumRegex.test(pwd)) {
     $('.middle').css({
      'background': '#FFC125'
     });
     $('.strong').css({
      'background': 'rgb(238, 238, 238)'
     });
     $('.result').text('中')
    } else {
     $('.weak').css({
      'background': '#EE4000'
     });
     $('.middle').css({
      'background': 'rgb(238, 238, 238)'
     });
     $('.strong').css({
      'background': 'rgb(238, 238, 238)'
     });
     $('.result').text('弱')
    }
   }
}
效果
密码强度为弱
密码强度为中:
密码强度为强
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
                                免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
                                如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
                            
                        暂无“javascript实现前端input密码输入强度验证”评论...
                                    更新动态
2025年10月31日
                                2025年10月31日
                    - 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
 
                         
                        


