這段JavaScript代碼比較實(shí)用,它完成用戶注冊(cè)時(shí)判斷用戶輸入密碼的強(qiáng)度,分強(qiáng)、弱、中三等級(jí),它可以根據(jù)用戶輸入的密碼顯示對(duì)應(yīng)的密碼強(qiáng)弱等級(jí),方便用戶改進(jìn)輸入 。

成都創(chuàng)新互聯(lián)公司是一家企業(yè)級(jí)云計(jì)算解決方案提供商,超15年IDC數(shù)據(jù)中心運(yùn)營(yíng)經(jīng)驗(yàn)。主營(yíng)GPU顯卡服務(wù)器,站群服務(wù)器,成都服務(wù)器托管,海外高防服務(wù)器,成都機(jī)柜租用,動(dòng)態(tài)撥號(hào)VPS,海外云手機(jī),海外云服務(wù)器,海外服務(wù)器租用托管等。
html
head
titleJS判斷密碼強(qiáng)度/title
script language=javascript
//判斷輸入密碼的類型
function CharMode(iN){
if (iN=48 iN =57) //數(shù)字
return 1;
if (iN=65 iN =90) //大寫
return 2;
if (iN=97 iN =122) //小寫
return 4;
else
return 8;
}
//bitTotal函數(shù)
//計(jì)算密碼模式
function bitTotal(num){
modes=0;
for (i=0;i4;i++){
if (num 1) modes++;
num=1;
}
return modes;
}
//返回強(qiáng)度級(jí)別
function checkStrong(sPW){
if (sPW.length=4)
return 0; //密碼太短
Modes=0;
for (i=0;isPW.length;i++){
//密碼模式
Modes|=CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}
//顯示顏色
function pwStrength(pwd){
O_color="#eeeeee";
L_color="#FF0000";
M_color="#FF9900";
H_color="#33CC00";
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=Mcolor=M_color;
Hcolor=O_color;
break;
default:
Lcolor=Mcolor=Hcolor=H_color;
}
}
document.getElementById("strength_L").style.background=Lcolor;
document.getElementById("strength_M").style.background=Mcolor;
document.getElementById("strength_H").style.background=Hcolor;
return;
}
/script
/head
body
form name=form1 action=""
密碼:input type=password size=8 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)
br密碼強(qiáng)度:
table width="210" border="1" cellspacing="0" cellpadding="1" bordercolor="#eeeeee" height="22" style='display:inline'
tr align="center" bgcolor="#f5f5f5"
td width="33%" id="strength_L"弱/td
td width="33%" id="strength_M"中/td
td width="33%" id="strength_H"強(qiáng)/td
/tr
/table
/form
/body
/html
使用組合的形式:
純數(shù)字 /\d+/
純字母小寫/[a-z]+/
純字母大寫/[A-Z]+/
純特殊符號(hào)/[~!@#\$%^*\(\)\{\};,.\?\/'"]/
假設(shè)1-4的權(quán)重分別為2,4,8,16;
則拿1-4分別檢測(cè)用戶的密碼,如果有,則或上相應(yīng)的權(quán)重。
然后看這個(gè)數(shù)字是處于2-30之間,然后根據(jù)這個(gè)數(shù)算出強(qiáng)度即可
/*
*@description 校驗(yàn)密碼強(qiáng)度的方法
*@param {String} val 待校驗(yàn)強(qiáng)度的密碼
*@return {Number} 密碼強(qiáng)度等級(jí),數(shù)字越大強(qiáng)度越高
*/
function checkPwd(val){
var res = 2;
// 長(zhǎng)度不低于6位和12位,強(qiáng)度分別+1和+2
res += val.length = 6 ? 1 : 0;
res += val.length = 12 ? 2 : 0;
// 包含小寫字母,強(qiáng)度+1或+2
res += /[a-z]/.test(val) ? (val.length 8 || /\d{4}/.test(val) ? 1 : 2) : 0;
// 包含大寫字母,強(qiáng)度+1或+2
res += /[A-Z]/.test(val) ? (val.length 8 || /\d{4}/.test(val) ? 1 : 2) : 0;
// 包含特殊字符,強(qiáng)度+1或+2
res += /[^a-zA-Z\s\d]/.test(val) ? (val.length 8 || /\d{4}/.test(val) ? 1 : 2) : 0;
return Math.floor(res / 2);
}
本文題目:javascript強(qiáng)度,javascript性能優(yōu)化
轉(zhuǎn)載源于:http://chinadenli.net/article12/dsgchdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、關(guān)鍵詞優(yōu)化、微信小程序、App開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)