1<script language=javascript>
2
3//CharMode函数
4//测试某个字符是属于哪一类.
5function CharMode(iN){
6if (iN>=48 && iN <=57) //数字
7return 1;
8if (iN>=65 && iN <=90) //大写字母
9return 2;
10if (iN>=97 && iN <=122) //小写
11return 4;
12else
13return 8; //特殊字符
14}
15
16//bitTotal函数
17//计算出当前密码当中一共有多少种模式
18function bitTotal(num){
19modes=0;
20for (i=0;i<4;i++){
21if (num & 1) modes++;
22num>>>=1;
23}
24return modes;
25}
26
27//checkStrong函数
28//返回密码的强度级别
29
30function checkStrong(sPW){
31if (sPW.length<=4)
32return 0; //密码太短
33Modes=0;
34for (i=0;i<sPW.length;i++){
35//测试每一个字符的类别并统计一共有多少种模式.
36Modes|=CharMode(sPW.charCodeAt(i));
37}
38
39return bitTotal(Modes);
40
41}
42
43//pwStrength函数
44//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
45
46function pwStrength(pwd){
47O_color="#eeeeee";
48L_color="#FF0000";
49M_color="#FF9900";
50H_color="#33CC00";
51if (pwd==null||pwd==''){
52Lcolor=Mcolor=Hcolor=O_color;
53}
54else{
55S_level=checkStrong(pwd);
56switch(S_level) {
57case 0:
58Lcolor=Mcolor=Hcolor=O_color;
59case 1:
60Lcolor=L_color;
61Mcolor=Hcolor=O_color;
62break;
63case 2:
64Lcolor=Mcolor=M_color;
65Hcolor=O_color;
66break;
67default:
68Lcolor=Mcolor=Hcolor=H_color;
69}
70}
71
72document.getElementById("strength_L").style.background=Lcolor;
73document.getElementById("strength_M").style.background=Mcolor;
74document.getElementById("strength_H").style.background=Hcolor;
75return;
76}
77
78</script>
79
80<form name=form1 action="" >
81输入密码:<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>
82<br>密码强度:
83<table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>
84<tr align="center" bgcolor="#eeeeee">
85
86<td width="33%" id="strength_L">弱</td>
87
88<td width="33%" id="strength_M">中</td>
89
90<td width="33%" id="strength_H">强</td>
91</tr>
92</table>
93</form>
94
2
3//CharMode函数
4//测试某个字符是属于哪一类.
5function CharMode(iN){
6if (iN>=48 && iN <=57) //数字
7return 1;
8if (iN>=65 && iN <=90) //大写字母
9return 2;
10if (iN>=97 && iN <=122) //小写
11return 4;
12else
13return 8; //特殊字符
14}
15
16//bitTotal函数
17//计算出当前密码当中一共有多少种模式
18function bitTotal(num){
19modes=0;
20for (i=0;i<4;i++){
21if (num & 1) modes++;
22num>>>=1;
23}
24return modes;
25}
26
27//checkStrong函数
28//返回密码的强度级别
29
30function checkStrong(sPW){
31if (sPW.length<=4)
32return 0; //密码太短
33Modes=0;
34for (i=0;i<sPW.length;i++){
35//测试每一个字符的类别并统计一共有多少种模式.
36Modes|=CharMode(sPW.charCodeAt(i));
37}
38
39return bitTotal(Modes);
40
41}
42
43//pwStrength函数
44//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
45
46function pwStrength(pwd){
47O_color="#eeeeee";
48L_color="#FF0000";
49M_color="#FF9900";
50H_color="#33CC00";
51if (pwd==null||pwd==''){
52Lcolor=Mcolor=Hcolor=O_color;
53}
54else{
55S_level=checkStrong(pwd);
56switch(S_level) {
57case 0:
58Lcolor=Mcolor=Hcolor=O_color;
59case 1:
60Lcolor=L_color;
61Mcolor=Hcolor=O_color;
62break;
63case 2:
64Lcolor=Mcolor=M_color;
65Hcolor=O_color;
66break;
67default:
68Lcolor=Mcolor=Hcolor=H_color;
69}
70}
71
72document.getElementById("strength_L").style.background=Lcolor;
73document.getElementById("strength_M").style.background=Mcolor;
74document.getElementById("strength_H").style.background=Hcolor;
75return;
76}
77
78</script>
79
80<form name=form1 action="" >
81输入密码:<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>
82<br>密码强度:
83<table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>
84<tr align="center" bgcolor="#eeeeee">
85
86<td width="33%" id="strength_L">弱</td>
87
88<td width="33%" id="strength_M">中</td>
89
90<td width="33%" id="strength_H">强</td>
91</tr>
92</table>
93</form>
94