发新话题
打印

用[Javascript]检测用户输入密码强度级别

用[Javascript]检测用户输入密码强度级别

我们在一些网站中有看到能对用户输入的密码进行安全检测,并显示出相应的提示信息( p* `$ N, X! U6 V1 u- @9 O* O; H9 \
现在就可以让你的网站更人性化,更有吸引力.(以下代码已通过测试,对其进行相应的修改就可以加入到你的网站中):$ S7 G* ^( f* }: N6 G6 j8 J/ k

+ v( j9 M9 [% U3 K<script language=javascript> / }7 |& y* z7 S% }2 g0 \: a# Y
//CharMode函数 + Q9 \+ S; |$ ~% h
//检测某个字符是属于哪一类. 2 L+ s: f0 Z& J7 e, m4 z
function CharMode(iN){ 5 [2 Y3 p& ^6 S
if (iN>=48 && iN <=57) //数字
" x5 D0 A9 U* u9 l8 K3 Sreturn 1;
  p" E+ m& ?6 o6 R# lif (iN>=65 && iN <=90) //大写字母 $ d2 ]: G2 R$ k6 t
return 2;
6 s6 t3 M) y( x+ x) s, x+ Kif (iN>=97 && iN <=122) //小写 0 J, W# \5 c) p- Z  X! u8 l
return 4; 4 n$ t6 W9 t( R( K- \0 p
else 2 Q, T6 u! K- K% Z9 F' L
return 8; //特殊字符
3 A, A( ~, ~5 _. C7 c}
: Q/ Z6 |7 M0 n* h. Q) k//bitTotal函数
4 _, [8 I7 y, r/ z+ @6 ^0 J1 P//计算出当前密码当中一共有多少种模式
' w0 B- {3 ^) N6 u+ f& afunction bitTotal(num){ * n9 Z, @% A. f
modes=0; 4 p. H- p) W* p% p! p. ?( e$ v
for (i=0;i<4;i++){ ; O* ~; Z" U" l4 v$ ~
if (num & 1) modes++; 5 h0 n# a* H, Y4 W% C
num>>>=1; , U2 F! a' x% t( f/ N$ p/ l
} / Y3 s4 b! s( q: Y9 a
return modes;
8 J& O% E6 H2 N9 J( U' j" Z: G5 Y}
0 a- h$ W* }0 H+ I! |//checkStrong函数
& x' [4 h8 L/ \- s//返回密码的强度级别
& Y/ n1 h, ?; P% T: z/ Gfunction checkStrong(sPW){ ; K1 B5 x0 ?2 \- `3 x$ B
if (sPW.length<=4) % W$ F. o4 ^, C
return 0; //密码太短   x6 v/ A$ z2 j
Modes=0;
# b5 E& ]- R. w: gfor (i=0;i<sPW.length;i++){
9 o1 g) M0 a/ B7 K3 l) w/ n//检测每一个字符的类别并统计一共有多少种模式.
" X! H) ^- m3 N2 S" _+ K, y( E2 pModes|=CharMode(sPW.charCodeAt(i)); " ]$ a- u% w3 @7 D$ _* _& ^
}
1 c7 b7 w7 C) U/ w  vreturn bitTotal(Modes);
. O, w# `$ C0 }- e} , O, x1 W! B5 S) m* b
//pwStrength函数 ) W: a' B9 v) q" N5 O7 G
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色 $ r7 X: w) V# k
function pwStrength(pwd){ ' v3 M/ g1 x, A& I
O_color="#eeeeee";
/ `/ f: G5 `' m2 y8 YL_color="#FF0000"; ! G2 c  T& g7 A: O( D& |
M_color="#FF9900";
: C0 y" P. f3 [+ vH_color="#33CC00"; : R" r7 `0 o" j7 P" L
if (pwd==null||pwd==''){
' C$ H" V/ B- G+ J  P  i' \Lcolor=Mcolor=Hcolor=O_color;
3 l* j0 B3 ]+ Q8 j3 E} 9 r: K4 ^, C1 A- {- c9 U
else{ ! c* }2 _5 @$ [, p, u3 U) r' w# v
S_level=checkStrong(pwd); ) c1 @% g( Z3 P/ s7 O$ @6 n& ]
switch(S_level) {
7 w! s/ s: \# |/ O% Rcase 0: . |0 b! `$ l8 g2 W* c
Lcolor=Mcolor=Hcolor=O_color;
' C- _- m; `3 j4 O- S( [case 1:
: T6 F) V. H+ }2 K+ ~. q4 y- KLcolor=L_color; % t6 c/ |5 F0 M* ^
Mcolor=Hcolor=O_color; : h( ?6 i6 A( @5 ]. o) }7 F
break; 1 x) b8 H9 E0 \% X: m
case 2: + p$ Z  R9 v0 ]& `, T8 l$ a' u- R+ N
Lcolor=Mcolor=M_color; * A) ]+ t7 R$ `% D) k! |
Hcolor=O_color;
3 \, j* v& C4 U& E* t. F& @6 ibreak;
8 q4 ~6 B+ q# Y0 Y2 edefault:   P- u2 ~; O( n, g1 B* r9 i: H2 r
Lcolor=Mcolor=Hcolor=H_color; " Q. k8 B$ ~+ R8 a) n2 D2 Y
} * B/ g* W0 l' x# h9 j# f
} ( R) [1 p" b1 X6 d
document.getElementById("strength_L").style.background=Lcolor;   p, E# g3 o  h; W
document.getElementById("strength_M").style.background=Mcolor; + O" U. `  P6 p: ?: D
document.getElementById("strength_H").style.background=Hcolor;
  t9 y5 D. ^, @  }4 V8 V- ^+ W4 n  Lreturn;
6 U, N3 E* y6 j9 L' e}
# l, g( _3 S+ F</script>
  m2 W6 r- E# u7 _<form name=form1 action="" > 0 `9 J" E1 X9 N
输入密码:<input type=password size=10 onKeyUp=pwStrength(this.value)
7 T2 @) q+ y; a+ z- jonBlur=pwStrength(this.value)> : q, ~) q3 D: {/ e
<br>密码强度:
4 V5 ]$ {* d9 I$ Q9 M0 D<table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
! |/ w9 J( U0 I1 S# N$ r) r& lheight="23" style='display:inline'>
% g- r0 x& K" F+ v<tr align="center" bgcolor="#eeeeee"> " T, p% |! o4 T- W& e" o9 {
<td width="33%" id="strength_L">弱</td> / t1 @0 K9 M8 E; ^0 b6 M
<td width="33%" id="strength_M">中</td> * _8 U- {! z, e+ s$ ~# \3 l
<td width="33%" id="strength_H">强</td> / j; U2 @6 _9 u+ w! l, w$ S; c
</tr> ( D  U$ j* }& h0 {3 M( O# U
</table> ' }. r% s- r: ~5 d5 ^5 M4 h" O# |
</form>
师父领进门
忽悠在个人

TOP

用[Javascript]检测用户输入密码强度级别

支持 !  

TOP

发新话题
手机号码所在地查询:
Google
IP地址: