A-A+

JS密码强度验证(显示密码强度)

2016年10月13日 前端设计 暂无评论 阅读 6 views 次

我们在很多网站注册时会有看到密码强度验证效果,下面我就来分离一下JS密码强度验证实现程序吧,有需要的朋友可参考。

原理:采用打分的机制,打分分为3类(基础分、加分、减分),先求出基础分,在计算加分的部分,最后减去要扣除的分数就为最终总分。
  
规则:

密码可输入类型(字符,字母大写,字母小写,特殊字符)。

基础分为,密码长度,一个长度为一分,大于18个字符都为18分;密码里面包含一种可输入类型,基础分加4分。

加分为,一种密码可输入类型的总数量大于等于2个,加分2分,如果总数量大于等于5,加分4分。

减分为,如果有连续重复的单个种类字符,则重复一次减1分。

总分50分。

0~10分:不合格(弱)

11~20分:一般

21~30分:中

31~40分:强

41~50分:安全

*分数范围可以自由调整和搭配,其实整个打分规则都可以根据需要修改

举例,代码如下:

  1. <script>  
  2. function testpass(password,username)  
  3. {  
  4.  var score = 0;  
  5.  if (password.length < 4 ) { return -4; }  
  6.  if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2}  
  7.  score += password.length * 4;  
  8.  score += ( repeat(1,password).length - password.length ) * 1;  
  9.  score += ( repeat(2,password).length - password.length ) * 1;  
  10.  score += ( repeat(3,password).length - password.length ) * 1;  
  11.  score += ( repeat(4,password).length - password.length ) * 1;  
  12.  if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;}  
  13.  if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;}  
  14.  if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;}  
  15.  if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;}  
  16.  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;}  
  17.  if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;}  
  18.  if (password.match(/^w+$/) || password.match(/^d+$/) ){ score -= 10;}  
  19.  if ( score < 0 ){score = 0;}  
  20.  if ( score > 100 ){ score = 100;}  
  21.  return score;  
  22.    
  23.  function repeat(len,str)  
  24.  {  
  25.   var res = "";   
  26.   for (var i = 0; i < str.length; i++ )   
  27.   {   
  28.    var repeated = true;   
  29.    for (var j = 0, max = str.length - i - len; j < len && j < max; j++)  
  30.    {  
  31.     repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len));   
  32.    }   
  33.    if (j < len) repeated = false;  
  34.    if (repeated) {   
  35.     i += len - 1;   
  36.     repeated = false;   
  37.    }   
  38.    else   
  39.    {  
  40.     res += str.charAt(i);   
  41.    }  
  42.   }   
  43.   return res;   
  44.  }  
  45. }  
  46. function checkpass(pass)  
  47. {  
  48.     var username = document.getElementById('username').value;  
  49.     var score = testpass(pass.value,username);  
  50.     var password_label = document.getElementById('password_label');  
  51.     if(score == -4)  
  52.     {  
  53.         password_label.innerHTML = '太短';  
  54.     }  
  55.     else if(score == -2)  
  56.     {  
  57.         password_label.innerHTML = '与用户名相同';  
  58.     }  
  59.     else  
  60.     {  
  61.         var color = score < 34 ? '#edabab' : (score < 68 ? '#ede3ab' : '#d3edab');  
  62.         var text = score < 34 ? '弱' : (score < 68 ? '一般' : '很好');  
  63.         var width = score + '%'; //xiariboke.net  
  64.         password_label.innerHTML = "<span style='width:"+width+";display:block;overflow:hidden;height:20px;line-height:20px;background:"+color+";'>"+text+"</span>";  
  65.     }  
  66. }  
  67. </script>  
  68.   
  69. 请输入用户名:<br>  
  70. <input type="text" class="inpt" name="username" style="width:160px" id="username" /><br>  
  71. 请输入密码:<br>  
  72. <input type="password" class="inpt" style="width:160px" onkeyup="javascript:checkpass(this)" name="pass" id="pass" /><br>  
  73. <span id="password_label" style="width:160px;border:1px solid #F0F0F0"></span>  
标签:

给我留言