在一些网站注册的时候经常可以看到密码强度提示,例如优酷: 今天自己做了一个JS密码强度判断,效果如下: 以下是代码: 复制代码 代码如下: JS判断密码强度 <BR>//判断输入密码的类型 <BR>function CharMode(iN){ <BR>if (iN>=48 && iN <=57) //数字 <BR>return 1; <BR>if (iN>=65 && iN <=90) //大写 <BR>return 2; <BR>if (iN>=97 && iN <=122) //小写 <BR>return 4; <BR>else <BR>return 8; <BR>} <BR>//bitTotal函数 <BR>//计算密码模式 <BR>function bitTotal(num){ <BR>modes=0; <BR>for (i=0;i<4;i++){ <BR>if (num & 1) modes++; <BR>num>>>=1; <BR>} <BR>return modes; <BR>} <BR>//返回强度级别 <BR>function checkStrong(sPW){ <BR>if (sPW.length<=4) <BR>return 0; //密码太短 <BR>Modes=0; <BR>for (i=0;i<sPW.length;i++){ <BR>//密码模式 <BR>Modes|=CharMode(sPW.charCodeAt(i)); <BR>} <BR>return bitTotal(Modes); <BR>} <br><br>//显示颜色 <BR>function pwStrength(pwd){ <BR>O_color="#eeeeee"; <BR>L_color="#FF0000"; <BR>M_color="#FF9900"; <BR>H_color="#33CC00"; <BR>if (pwd==null||pwd==''){ <BR>Lcolor=Mcolor=Hcolor=O_color; <BR>} <BR>else{ <BR>S_level=checkStrong(pwd); <BR>switch(S_level) { <BR>case 0: <BR>Lcolor=Mcolor=Hcolor=O_color; <BR>case 1: <BR>Lcolor=L_color; <BR>Mcolor=Hcolor=O_color; <BR>break; <BR>case 2: <BR>Lcolor=Mcolor=M_color; <BR>Hcolor=O_color; <BR>break; <BR>default: <BR>Lcolor=Mcolor=Hcolor=H_color; <BR>} <BR>} <BR>document.getElementById("strength_L").style.background=Lcolor; <BR>document.getElementById("strength_M").style.background=Mcolor; <BR>document.getElementById("strength_H").style.background=Hcolor; <BR>return; <BR>} <BR> 密码: 密码强度: 弱 中 强