html代码:
1
js代码:
1 function pwdStrength(pwd) { 2 O_color = "#eeeeee"; 3 L_color = "#FF0000"; 4 M_color = "#FF9900"; 5 H_color = "#33CC00"; 6 var level = 0, strength = "O"; 7 if (pwd == null || pwd == '') { 8 strength = "O"; 9 Lcolor = Mcolor = Hcolor = O_color;10 }11 else {12 var mode = 0;13 if (pwd.length <= 4)14 mode = 0;15 else {16 for (i = 0; i < pwd.length; i++) {17 var charMode, charCode;18 charCode = pwd.charCodeAt(i);19 // 判断输入密码的类型20 if (charCode >= 48 && charCode <= 57) //数字 21 charMode = 1;22 else if (charCode >= 65 && charCode <= 90) //大写 23 charMode = 2;24 else if (charCode >= 97 && charCode <= 122) //小写 25 charMode = 4;26 else27 charMode = 8;28 mode |= charMode;29 }30 // 计算密码模式31 level = 0;32 for (i = 0; i < 4; i++) {33 if (mode & 1)34 level++;35 mode >>>= 1;36 }37 }38 switch (level) {39 case 0:40 strength = "O";41 Lcolor = Mcolor = Hcolor = O_color;42 break;43 case 1:44 strength = "L";45 Lcolor = L_color;46 Mcolor = Hcolor = O_color;47 break;48 case 2:49 strength = "M";50 Lcolor = Mcolor = M_color;51 Hcolor = O_color;52 break;53 default:54 strength = "H";55 Lcolor = Mcolor = Hcolor = H_color;56 break;57 }58 }59 document.getElementById("strength_L").style.background = Lcolor;60 document.getElementById("strength_M").style.background = Mcolor;61 document.getElementById("strength_H").style.background = Hcolor;62 return strength;63 }