在某些網站註冊的時候常常可以看到密碼強度提示,例如優酷: 今天自己做了一個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; 1: <BR>Lcolor=L_color; <BR>Mcolor=Hcolor=O_color; <BR>break; <BR>case 2: <BR>Lcolor=Mcolor=M_color; <BR>Hcolor=O_color <🎜break>; <BR>default: <BR>Lcolor=Mcolor=Hcolor=H_color; <BR>} <BR>} <BR>document.getElementById("strength_L").style.background=Lcolor; <BR>document.getElementById(ument." strength_M").style.background=Mcolor; <BR>document.getElementById("strength_H").style.background=Hcolor; <BR>return; <BR>} <BR> 密碼: 密碼強度: 弱 中 強