密码强度
<script type="text/javascript">
$('#newpassword').click(function () {
$('#pwdTip').show();
});
//测试某个字符是属于哪一类.
function CharMode(iN) {
if (iN >= 48 && iN <= 57) //数字
return 1;
if (iN >= 65 && iN <= 90) //大写字母
return 2;
if (iN >= 97 && iN <= 122) //小写
return 2;
else
return 8; //特殊字符
}
//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
modes = 0;
for (i = 0; i < 4; i++) {
if (num & 1) modes++;
num >>>= 1;
}
return modes;
}
//密码强度判断
function checkStrong(sPW) {
if (sPW.length <= 6) {
return 0; //密码太短
}
if (sPW.length == 6) {
return 1; //密码太短
}
Modes = 0;
for (i = 0; i < sPW.length; i++) {
//测试每一个字符的类别并统计一共有多少种模式.
Modes |= CharMode(sPW.charCodeAt(i));
}
var modes = bitTotal(Modes);
if (modes == 1) {
return 1;
}
if (modes == 2) {
if (sPW.length >= 7 && sPW.length <= 16) {
return 2;
}
}
if (modes == 3) {
if (sPW.length == 7 || sPW.length == 8) {
return 2;
}
if (sPW.length >= 9 && sPW.length <= 16) {
return 5;
}
}
}
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwdStrength(pwd) {
O_color = "";
//L_color = "week";
//M_color = "normal";
//H_color = "strong";
L_color = "strength-l";
M_color = "strength-m";
H_color = "strength-r";
M_color0 = "normal0";
H_color0 = "strong0";
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
}
else {
S_level = checkStrong(pwd);
switch (S_level) {
case 0:
Lcolor = Mcolor = Hcolor = O_color;
case 1:
Lcolor = L_color;
Mcolor = Hcolor = O_color;
break;
case 2:
Lcolor = M_color0;
Mcolor = M_color;
Hcolor = O_color;
break;
default:
Lcolor = Mcolor = H_color0;
Hcolor = H_color;
break;
}
}
$("#strength_L").attr('class', Lcolor);
$("#strength_M").attr('class', Mcolor);
$("#strength_H").attr('class', Hcolor);
return;
}
</script>