//CharMode函數
//測試某個字符是屬於哪一類.
$(function () {
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; //特殊字符
}
//bitTotal函數
//計算出當前密碼當中一共有多少種模式
function bitTotal(num) {
modes = 0;
for (i = 0; i < 4; i++) {
if (num & 1) modes++;
num >>>= 1;
}
return modes;
}
//checkStrong函數
//返回密碼的強度級別
function checkStrong(sPW) {
Modes = 0; //輸入的字符種類有幾種如:a1兩種aA_d三種
for (i = 0; i < sPW.length; i++) {
//測試每一個字符的類別並統計一共有多少種模式.
Modes |= CharMode(sPW.charCodeAt(i));
}
Modes = bitTotal(Modes); //由幾種字符組成
var pwdLength = sPW.length; //密碼長度
var level = 0; //密碼強度級別
if (pwdLength < 8 && Modes <= 2)
level = 0;
if ((pwdLength < 10 && Modes >= 3) || (pwdLength >= 8 && Modes == 2))
level = 1;
if (pwdLength >= 10 && Modes >= 3)
level = 2;
return level;
}
//pwStrength函數
//根據pwd強度改變css樣式
function pwStrength(pwd) {
var $strength_L = $("#pwd_strength");
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
}
else {
$strength_L.removeClass("security3");
$strength_L.removeClass("security2");
$strength_L.removeClass("security1");
S_level = checkStrong(pwd);
switch (S_level) {
case 0: //低
$strength_L.addClass("security3");
break;
case 1: //中
$strength_L.addClass("security2");
break;
case 2: //高
$strength_L.addClass("security1");
break;
default:
$strength_L.addClass("security3");
}
}
return;
}
//pwd事件觸發:當用戶放開鍵盤或密碼輸入框失去焦點時,根據不同的級別顯示不同的顏色
$("#pwd_new").keyup(function () {
pwStrength(this.value);
})
//失去光標事件
$("#pwd_new").blur(function () {
pwStrength(this.value);
})
})
JS驗證密碼強度插件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.