验证密码强度的js

代码
 1  <script type="text/javascript">
 2    function CharMode(iN){
 3         if (iN>=48 && iN <=57)
 4         return 1
 5         if (iN>=65 && iN <=90)
 6         return 2;
 7         if (iN>=97 && iN <=122)
 8         return 4;
 9         else
10         return 8;
11      }
12    function bitTotal(num){
13         modes=0;
14         for (i=0;i<4;i++){
15         if (num & 1) modes++;
16         num>>>=1;
17         }
18         return modes;
19       }
20 
21         function checkStrong(sPW){
22             if (sPW.length<=4)
23             return 0;
24             Modes=0;
25             for (i=0;i<sPW.length;i++){
26             Modes|=CharMode(sPW.charCodeAt(i));
27             }
28               return bitTotal(Modes);
29         } 
30         function pwStrength(pwd){
31             O_color="#eeeeee";
32             L_color="#FF0000";
33             M_color="#FF9900";
34             H_color="#33CC00";
35         if (pwd==null||pwd==''){
36             Lcolor=Mcolor=Hcolor=O_color;
37         } 
38         else{
39             S_level=checkStrong(pwd);
40             switch(S_level) {
41             case 0:
42             Lcolor=Mcolor=Hcolor=O_color; 
43             case 1:
44             Lcolor=L_color;
45             Mcolor=Hcolor=O_color;
46             break;
47             case 2:
48             Lcolor=Mcolor=M_color;
49             Hcolor=O_color;
50             break;
51             default:
52             Lcolor=Mcolor=Hcolor=H_color;
53          }
54         } 
55         document.getElementById("strength_L").style.background=Lcolor;
56         document.getElementById("strength_M").style.background=Mcolor;
57         document.getElementById("strength_H").style.background=Hcolor;
58         return;
59      }
60     </script>
61 
62 </head>
63 <body>
64     <form id="form1" runat="server">
65     <div>
66         <input id="Password1" type="password" onkeyup="pwStrength(this.value)" />
67         <table style="display: inline" bordercolor="#cfe8fe" height="20 " cellspacing="0"
68             cellpadding="1" width="160" border="1">
69             <tr>
70                 <td>
71                 </td>
72             </tr>
73             <tr align="center" bgcolor="#ffffff">
74                 <td width="33%" id="strength_L">
75                     弱
76                 </td>
77                 <td width="33%" id="strength_M">
78                     中
79                 </td>
80                 <td width="33%" id="strength_H">
81                     强
82                 </td>
83             </tr>
84         </table>
85     </div>
86     </form>
87 </body>
88 </html>


原文地址:https://www.cnblogs.com/wanglinglong/p/1624805.html