[轉]用javascript實現評估用戶輸入密碼的強度


<script language=javascript>

//程序設計:環球萬維 netInter.cn
//
本程序是環球萬維原創程序,若需轉載,請註明網址及出處,謝謝.
//
以上信息與文章正文是不可分割的一部分,所以如果您要轉載本文章,您必須保留以上信息.

//CharMode函數
//
測試某個字符是屬於哪一類.
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){
if (sPW.length<=4)
return 0//密碼太短
Modes=0;
for (i=0;i<sPW.length;i++){
//測試每一個字符的類別並統計一共有多少種模式.
Modes|=CharMode(sPW.charCodeAt(i));
}


return bitTotal(Modes);

}
 

//pwStrength函數
//
當用戶放開鍵盤或密碼輸入框失去焦點時,根據不同的級別顯示不同的顏色

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>

<form name=form1 action="" >
輸入密碼:
<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>
<br>密碼強度:
<table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>
<tr align="center" bgcolor="#eeeeee"> 

<td width="33%" id="strength_L"></td>

<td width="33%" id="strength_M"></td>

<td width="33%" id="strength_H"></td>
</tr>
</table>

</form>



easonyo 發表在 痞客邦 留言(0) 人氣()

- 函數有兩個參數,功能如下:
obj: img標籤組的父容器,類型為DOM對象;
oEvent:event對象。
這 個函數的優點是html代碼可以很簡潔,使用圖片也可以很少,只需要兩張圖片。事件句柄只需要寫在img的父容器上即可。演示用的圖片我用的是絕對地址, 各位在使用的時候改成網站的相對地址就可以了。當我們點擊的時候,我用的是個alert事件。事實上, 把this._num+1這個數字寫入到數據庫中,作為評分的依據就可以了
需要的兩張圖片:
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>腳本之家_www.jb51.net_阿當製作選星星打分</title>
<style type="text/css">
.starWrapper{border:1px solid #FFCC00;padding:5px;width:70px;}
.starWrapper img{cursor:pointer;}
</style>
<script type="text/javascript">
function rate(obj,oEvent){
//==================
// 圖片地址設置
//==================
var imgSrc = 'http://www.jb51.net/upload/20080508122008586.gif'; //沒有填色的星星
var imgSrc_2 = 'http://www.jb51.net/upload/20080508122010810.gif'; //打分後有顏色的星星
//---------------------------------------------------------------------------
if(obj.rateFlag) return;
var e = oEvent || window.event;
var target = e.target || e.srcElement; 
var imgArray = obj.getElementsByTagName("img");
for(var i=0;i<imgArray.length;i++){
   imgArray[i]._num = i;
   imgArray[i].onclick=function(){
    if(obj.rateFlag) return;
    obj.rateFlag=true;
    alert(this._num+1);       //this._num+1這個數字寫入到數據庫中,作為評分的依據
   };
}
if(target.tagName=="IMG"){
   for(var j=0;j<imgArray.length;j++){
    if(j<=target._num){
     imgArray[j].src=imgSrc_2;
    } else {
     imgArray[j].src=imgSrc;
    }
   }
} else {
   for(var k=0;k<imgArray.length;k++){
    imgArray[k].src=imgSrc;
   }
}
}
</script>
<body>
<p class="starWrapper" onmouseover="rate(this,event)"> 
<img src="http://www.jb51.net/upload/20080508122008586.gif" title="很爛" />
<img src="http://www.jb51.net/upload/20080508122008586.gif" title="一般" /> <img src="http://www.jb51.net/upload/20080508122008586.gif" title="還好" /> <img src="http://www.jb51.net/upload/20080508122008586.gif" title="較好" />

easonyo 發表在 痞客邦 留言(2) 人氣()


有了 JavaScript 你可以取得及處理所有的 HTML
DOM物件(Document Object Model ,文件物件模型簡稱 DOM)

easonyo 發表在 痞客邦 留言(0) 人氣()


字串物件是用在文字上

easonyo 發表在 痞客邦 留言(0) 人氣()


內建的計算物件包括數學的常數及函數

easonyo 發表在 痞客邦 留言(0) 人氣()


日期物件是用在日期與時間上

easonyo 發表在 痞客邦 留言(0) 人氣()


布林物件是一個包含了布林值的物件,他是用來將非布林值置換成布林值:

easonyo 發表在 痞客邦 留言(0) 人氣()


陣列物件 (Array object)
是在一個單一的變數裏儲存一組的值(可以是字串、數值,或是另一個物件)

easonyo 發表在 痞客邦 留言(0) 人氣()


透過 JavaScript, 是可以執行一些程式碼而不須立即執行已呼叫的功能,而在特訂的時間差異後才執行,這就叫時間事件。

easonyo 發表在 痞客邦 留言(0) 人氣()

Planets

圖片設定(image-map)是一種可點選圖片的區域

easonyo 發表在 痞客邦 留言(0) 人氣()


透過 JavaScript 我們可以建立動態的圖片。

easonyo 發表在 痞客邦 留言(0) 人氣()


JavaScript 能在網頁資料傳送至網路伺器之前,在HTML的格式裏先驗證輸入的資料

easonyo 發表在 痞客邦 留言(0) 人氣()

« 1 2 3 4
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。