javascript密码强度判断

来源:互联网 发布:mangos srp算法 编辑:程序博客网 时间:2024/04/30 01:46
  1. -------javascript代码:-------------------  
  2. <script type="text/javascript">  
  3.         //CharMode函数   
  4.         //测试某个字符是属于哪一类.   
  5.         function CharMode(iN){   
  6.             if (iN>=48 && iN <=57) //数字   
  7.             return 1;   
  8.             if (iN>=65 && iN <=90) //大写字母   
  9.             return 2;   
  10.             if (iN>=97 && iN <=122) //小写   
  11.             return 4;   
  12.             else   
  13.             return 8; //特殊字符   
  14.         }   
  15.   
  16.         //bitTotal函数   
  17.         //计算出当前密码当中一共有多少种模式   
  18.         function bitTotal(num){   
  19.             modes=0;   
  20.             for (i=0;i<4;i++){   
  21.             if (num & 1) modes++;   
  22.             num>>>=1;   
  23.             }   
  24.             return modes;   
  25.         }   
  26.   
  27.         //checkStrong函数   
  28.         //返回密码的强度级别   
  29.   
  30.         function checkStrong(sPW){   
  31.             if (sPW.length<=4)   
  32.             return 0; //密码太短   
  33.             Modes=0;   
  34.             for (i=0;i<sPW.length;i++){   
  35.             //测试每一个字符的类别并统计一共有多少种模式.   
  36.             Modes|=CharMode(sPW.charCodeAt(i));   
  37.         }   
  38.   
  39.         return bitTotal(Modes);   
  40.   
  41.         }   
  42.   
  43.         //pwStrength函数   
  44.         //当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色   
  45.   
  46.         function pwStrength(pwd){   
  47.         O_color="#eeeeee";   
  48.         L_color="#FF0000";   
  49.         M_color="#FF9900";   
  50.         H_color="#33CC00";   
  51.         if (pwd==null||pwd==''){   
  52.         Lcolor=Mcolor=Hcolor=O_color;   
  53.         }   
  54.         else{   
  55.         S_level=checkStrong(pwd);   
  56.         switch(S_level) {   
  57.         case 0:   
  58.         Lcolor=Mcolor=Hcolor=O_color;   
  59.         case 1:   
  60.         Lcolor=L_color;   
  61.         Mcolor=Hcolor=O_color;   
  62.         break;   
  63.         case 2:   
  64.         Lcolor=Mcolor=M_color;   
  65.         Hcolor=O_color;   
  66.         break;   
  67.         default:   
  68.         Lcolor=Mcolor=Hcolor=H_color;   
  69.         }   
  70.         }   
  71.   
  72.         document.getElementById("strength_L").style.background=Lcolor;   
  73.         document.getElementById("strength_M").style.background=Mcolor;   
  74.         document.getElementById("strength_H").style.background=Hcolor;   
  75.         return;   
  76.         }  
  77.  </script>  

html页面:

[html] view plaincopy
  1. <form name=form1 action="" >   
  2. 输入密码:<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>   
  3. <br>密码强度:   
  4. <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>   
  5. <tr align="center" bgcolor="#eeeeee">   
  6. <td width="33%" id="strength_L"></td>   
  7. <td width="33%" id="strength_M"></td>   
  8. <td width="33%" id="strength_H"></td>   
  9. </tr>   
  10. </table>   
  11. </form>  
0 0