js校验密码强度 博客分类: javascript js校验密码强度
程序员文章站
2024-03-14 22:18:35
...
js代码校验密码的强度
checkPassword.js
checkPassword.html
checkPassword.js
//检查密码合法性 function checkPwd(content) { O_color="#ccc"; L_color="#FF0000"; M_color="#FF9900"; H_color="#33CC00"; if (content==null||content==''){ Lcolor=Mcolor=Hcolor=O_color; } else { var S_level = checkStrong(content);//检查密码合法性(1) 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; } } $("#strength_L").css({'background':Lcolor}); $("#strength_M").css({'background':Mcolor}); $("#strength_H").css({'background':Hcolor}); return; } //检查密码合法性(1) 返回密码的强度级别 function checkStrong(sPW){ if (sPW.length<6) return 0; //密码太短 Modes=0; for (i=0;i<sPW.length;i++){ //测试每一个字符的类别并统计一共有多少种模式. Modes |= charMode(sPW.charCodeAt(i)); //验证字符的类型(2) } return bitTotal(Modes); //计算出当前密码当中一共有多少种模式(3) } //验证字符的类型(2) function CharMode(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; //特殊字符 } //计算出当前密码当中一共有多少种模式 (3) function bitTotal(num){ modes=0; for (i=0;i<4;i++){ if (num & 1) modes++; num>>>=1; } return modes; }
checkPassword.html
<div class="bd" style="margin:50px 450px;width: 1000px;"> <div class="dzcz_voice"> <div class="xbos-panel"> <table class="FM FM_tp1 AddLinkman"> <tbody> <tr> <td class="lab"><span style="font-size: 13px;">新密码:</span></td> <td class="inp"><input style="width:190px" value="" type="password" name="newPwd" id="newPwd" maxlength="128" class="INP_text"/></td> </tr> <tr> <td class="lab"><span style="font-size:13px;">密码强度:</span></td> <td style="padding-left: 2px;"> <table style="display: inline;cellSpacing :0px;" > <tr> <td id="strength_L" class="pwdStrong">弱 </td> <td id="strength_M" class="pwdStrong">中 </td> <td id="strength_H" class="pwdStrong">强</td> </tr> </table> </td> </tr> <tr> <td class="lab"><span style="font-size:13px;">新密码确认:</span></td> <td class="inp"><input style="width:190px" value="" type="password" name="newPwd2" id="newPwd2" maxlength="128" class="INP_text"></td> </tr> <tr> <td class="inp" colspan="2" style="text-align: center;float: none; "><a id="checkPassword" href="javascript:;" class="enter_btn" style="margin-left: 90px;">确认</a></td> </tr> </tbody> </table> </div> </div> </div>