日期:2014-05-16  浏览次数:20372 次

Javascript实现评估密码强度

<script language=javascript>
<!--
function Charstring(iN){             
if (iN>=48 && iN <=57)               
return 1;
if (iN>=65 && iN <=90)             
return 2;
if (iN>=97 && iN <=122)            
return 4;
else
return 8;        
}

function modetotal(num){     
modes=0;
for (i=0;i<4;i++){
if (num & 1)
 modes++;
 num>>>=1;
}
return modes;
}

function checkpwd(spwd){  
if (spwd.length<=4)
return 0;                        
Modes=0;
for (i=0;i<spwd.length;i++){        
 Modes|=Charstring(spwd.charCodeAt(i));
}
return modetotal(Modes);
}

function pwdstrong(pwd){      
O_color="#cccccc";             
L_color="#ffff00";              
M_color="#DC440F";               
H_color="#FF0000";                 
if (pwd==null||pwd==''){            
 Lcolor=Mcolor=Hcolor=O_color;  
}
else{
 S_level=checkpwd(pwd);       
 switch(S_level) {
 case 0:
  Lcolor=Mcolor=Hcolor=O_color; 
 case 1:
  Lcolor=L_color;
  Mcolor=Hcolor=O_color;          
  break;
 case 2:
  Lcolor=Mcolor=M_color;
  Hcolor=O_color;          
  break;
 default:
 Lcolor=Mcolor=Hcolor=H_color;
}
}
document.getElementById("pwd_L").style.background=Lcolor;   
document.getElementById("pwd_M").style.background=Mcolor;
document.getElementById("pwd_H").style.background=Hcolor;
return;
}
//-->
</script>

<form>
输入密码:<input type=password size=30 onKeyUp=pwdstrong(this.value) onBlur=pwdstrong(this.value)>
<br>密码强度:
<table border=1 cellspacing=0 cellpadding=1 bordercolor=#cccccc height=22 style='display:inline'>
 <tr align="center" bgcolor="#eeeeee">
  <td width=40 id="pwd_L">弱</td>
  <td width=40 id="pwd_M">中</td>
  <td width=40 id="pwd_H">强</td>
 </tr>
</table>
</form>