1
<script language=javascript>
2
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
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

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94
