javascripit實現(xiàn)密碼強度檢測代碼分享
更新時間:2013年12月12日 11:47:11 作者:
這篇文章主要介紹了javascripit實現(xiàn)密碼強度檢測,大家參考使用吧
復制代碼 代碼如下:
<!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=gb2312" />
<title>
密碼強度檢測
</title>
<style type="text/css">
body{font:12px/1.5 Arial;} input{float:left;font-size:12px;width:150px;font-family:arial;border:1px
solid #ccc;padding:3px;} input.correct{border:1px solid green;} input.error{border:1px
solid red;} #tips{float:left;margin:2px 0 0 20px;} #tips span{float:left;width:50px;height:20px;color:#fff;overflow:hidden;background:#ccc;margin-right:2px;line-height:20px;text-align:center;}
#tips.s1 .active{background:#f30;} #tips.s2 .active{background:#fc0;} #tips.s3
.active{background:#cc0;} #tips.s4 .active{background:#090;}
</style>
<script type="text/javascript">
window.onload = function() {
var oTips = document.getElementById("tips");
var oInput = document.getElementsByTagName("input")[0];
var aSpan = oTips.getElementsByTagName("span");
var aStr = ["弱", "中", "強", "非常好"];
var i = 0;
oInput.onkeyup = oInput.onfocus = oInput.onblur = function() {
var index = checkStrong(this.value);
this.className = index ? "correct": "error";
oTips.className = "s" + index;
for (i = 0; i < aSpan.length; i++) aSpan[i].className = aSpan[i].innerHTML = "";
index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1])
}
};
/** 強度規(guī)則
+ ------------------------------------------------------- +
1) 任何少于6個字符的組合,弱;任何字符數(shù)的同類字符組合,弱;
2) 任何字符數(shù)的兩類字符組合,中;
3) 12位字符數(shù)以下的三類或四類字符組合,強;
4) 12位字符數(shù)以上的三類或四類字符組合,非常好。
+ ------------------------------------------------------- +
**/
//檢測密碼強度
function checkStrong(sValue) {
var modes = 0;
if (sValue.length < 6) return modes;
if (/\d/.test(sValue)) modes++; //數(shù)字
if (/[a-z]/.test(sValue)) modes++; //小寫
if (/[A-Z]/.test(sValue)) modes++; //大寫
if (/\W/.test(sValue)) modes++; //特殊字符
switch (modes) {
case 1:
return 1;
break;
case 2:
return 2;
case 3:
case 4:
return sValue.length < 12 ? 3 : 4
break;
}
}
</script>
</head>
<body>
<input type="password" value="" maxlength="16" />
<div id="tips">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</body>
</html>
相關文章
JavaScript canvas實現(xiàn)雪花隨機動態(tài)飄落
這篇文章主要為大家詳細介紹了JavaScript canvas實現(xiàn)雪花隨機動態(tài)飄落,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02僅Firefox中鏈接A無法實現(xiàn)模擬點擊以觸發(fā)其默認行為
偶然發(fā)現(xiàn)之前寫的事件模塊在Firefox5中無法觸發(fā)A的默認行為了。IE/Opera/Firefox5中A具有click方法,因此模擬點擊直接調用click方法即可。2011-07-07