js實(shí)現(xiàn)密碼強(qiáng)度檢驗(yàn)
最近一直在做通行證項(xiàng)目,里面的注冊(cè)模塊中輸入密碼需要顯示密碼強(qiáng)度(低中高)。今天就把做的效果給大家分享下,代碼沒(méi)有網(wǎng)上搜索的那么復(fù)雜,能夠滿(mǎn)足一般的需求。
html 代碼如下:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>密碼強(qiáng)度</title>
<style type="text/css">
#passStrength{height:6px;width:120px;border:1px solid #ccc;padding:2px;}
.strengthLv1{background:red;height:6px;width:40px;}
.strengthLv2{background:orange;height:6px;width:80px;}
.strengthLv3{background:green;height:6px;width:120px;}
</style>
</head>
<body>
<input type="password" name="pass" id="pass" maxlength="16"/>
<div class="pass-wrap">
<em>密碼強(qiáng)度:</em>
<div id="passStrength"></div>
</div>
</body>
</html>
<script type="text/javascript" src="js/passwordStrength.js"></script>
<script type="text/javascript">
new PasswordStrength('pass','passStrength');
</script>
js 代碼如下:
function PasswordStrength(passwordID,strengthID){
this.init(strengthID);
var _this = this;
document.getElementById(passwordID).onkeyup = function(){
_this.checkStrength(this.value);
}
};
PasswordStrength.prototype.init = function(strengthID){
var id = document.getElementById(strengthID);
var div = document.createElement('div');
var strong = document.createElement('strong');
this.oStrength = id.appendChild(div);
this.oStrengthTxt = id.parentNode.appendChild(strong);
};
PasswordStrength.prototype.checkStrength = function (val){
var aLvTxt = ['','低','中','高'];
var lv = 0;
if(val.match(/[a-z]/g)){lv++;}
if(val.match(/[0-9]/g)){lv++;}
if(val.match(/(.[^a-z0-9])/g)){lv++;}
if(val.length < 6){lv=0;}
if(lv > 3){lv=3;}
this.oStrength.className = 'strengthLv' + lv;
this.oStrengthTxt.innerHTML = aLvTxt[lv];
};
效果圖:

使用說(shuō)明:
1、對(duì)象的第一個(gè)參數(shù)是密碼輸入框的 id,第二個(gè)參數(shù)是密碼強(qiáng)度長(zhǎng)條的 id。
2、checkStrength 方法中可以自定義密碼強(qiáng)度的規(guī)則。
3、密碼強(qiáng)度顯示低中高分別對(duì)應(yīng) 3 個(gè) css 樣式(strengthLv1、strengthLv2、strengthLv3)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript對(duì)下拉列表框(select)的操作實(shí)例講解
這篇文章主要介紹了javascript對(duì)下拉列表框(select)的操作。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
javascript 復(fù)雜的嵌套環(huán)境中輸出單引號(hào)和雙引號(hào)
如果簡(jiǎn)單的嵌套一般都是外面用雙引號(hào),則里面用單引號(hào),反之亦同,如果特別負(fù)責(zé)的嵌套大家看下如下的方法。2009-05-05
對(duì)象特征檢測(cè)法判斷瀏覽器對(duì)javascript對(duì)象的支持
就是將需要檢測(cè)的方法/對(duì)象作為if語(yǔ)句的判斷條件,具體做法如下2009-07-07
在Ajax中使用Flash實(shí)現(xiàn)跨域數(shù)據(jù)讀取的實(shí)現(xiàn)方法
今天,小子再提供一種使用Flash進(jìn)行跨域操作的方法。眾所周之,其實(shí)Flash的跨域操作也是有限制的,不過(guò),F(xiàn)lash的跨域配置比簡(jiǎn)單,只需要在站點(diǎn)根目錄下放置crossdomain.xml即可。2010-12-12

