<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>密码安全强度等级检测</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="张红君,www.iered.com">
<style type="text/css">
input.texts{width:80px;colo:black;font-size:12px;height:20px;}
.Init{width:40px;height:18px;color:black;font-size:12px;font-weight:bold;background-color:#EFEFEF;}
div,span,td,body{color:black;font-size:12px;font-weight:bold;}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
/*
*************************************************
****密码安全强度等级检测*********************
****作者:张红君
http://www.iered.com*******
****时间:2006-11-25****************************
*************************************************
*/
var BgArray = new Array(3);
BgArray[0] = "red";
BgArray[1] = "yellow";
BgArray[2] = "green";
var TextArray = new Array(3);
TextArray[0] = "white";
TextArray[1] = "green";
TextArray[2] = "red";
var TopBg = "blue",TextBg="#EFEFEF",TextColor="black";
function get_id(id)
{
if (typeof(id) != "string" || id == "") return null;
if (document.getElementById) return document.getElementById(id);
if (document.all) return document.all(id);
try {return eval(id);} catch(e){ return null;}
}
function OnlyNumberOrLetter(){
return (((event.keyCode >= 48) && (event.keyCode <= 57)) || ((event.keyCode >= 65) && (event.keyCode <= 90)) || ((event.keyCode >= 97) && (event.keyCode <= 122)));
}
function IsNum(){
return ((event.keyCode >= 48) && (event.keyCode <= 57));
}
//去除左右空格
function trimlr(ss)
{var str=ss;
for(i=0;str.charAt(i)==" "|| str.charAt(i)==" ";i++)
{
}
str=str.substring(i,str.length);
for(i=str.length-1;str.charAt(i)==" "|| str.charAt(i)==" ";i--)
{}
str=str.substring(0,i+1);
return str;
}
function isEmp(str)
{
var emp=trimlr(str);
if (emp.length<=0 || emp=="null" || emp==null) return true;
return false;
}
function isNumber(nu){//数字
var regu = "^[0-9]+$";
var re = new RegExp(regu);
return re.test(nu);
}
function isLetter(ts){//字母
var regu = "^[a-zA-Z]+$";
var re = new RegExp(regu);
return re.test(ts);
}
function isNumberOrLetter(ts){//数字或字母
var regu = "^[0-9a-zA-Z]+$";
var re = new RegExp(regu);
return re.test(ts);
}
function CkeckPasswordSalf(pid)
{var pStr,pLen,Salf,NumberLen,LetterLen;
pStr=trimlr(get_id(pid).value);
pLen=pStr.length;NumberLen=0;LetterLen=0;
if (isEmp(pStr) || pLen<=3) {Salf=0;}
else{
for (var i=0; i<pLen; i++){
if (isNumber(pStr.charAt(i))){NumberLen++;}
else{
if (isLetter(pStr.charAt(i))){LetterLen++;}}
}// End For
Salf=getPasswordRank(NumberLen,LetterLen);
}
for (var s=0; s<=Salf; s++){
get_id("p"+s).style.background = BgArray[Salf];
get_id("p"+s).style.color = TextArray[Salf];}
for (var j=0; j<3; j++)
{if (j==Salf)
{get_id("s"+j).style.background = TopBg;}
else{if (j>Salf){
get_id("p"+j).style.background = TextBg;
get_id("p"+j).style.color = TextColor;}
get_id("s"+j).style.background = "";
}
}
//密码为空时,对检测结果进行复位
if (pStr==""){get_id("s0").style.background = "";get_id("p0").style.background = TextBg;get_id("p0").style.color = TextColor;}
//显示密码并统计密码组成,应用时可删除以下三行代码
get_id("pw2").value=pStr;
get_id("pwn").innerText=NumberLen+"个";
get_id("pwl").innerText=LetterLen+"个";
return true;
}
//得到密码安全等级
function getPasswordRank(NumLen,LetLen)
{var nTotal=NumLen,lTotal=LetLen;
var nlt=parseInt(nTotal+lTotal);
//密码如果全是数字或全是字母
if (parseInt(nTotal/nlt)==1 || parseInt(lTotal/nlt)==1)
{if (nlt>15){return 2;}//大于15位,强
else{if (nlt>9 && nlt<=15){return 1;}//10-15位,中
else{return 0;}}//4-9位,弱
}else{//密码如果是数字和字母混合组成
if (nlt>8){return 2;}//大于8位,强
else{if (nlt>5 && nlt<=8){return 1;}//6-8位,中
else{return 0;}}//4-5位,弱
}
}
//-->
</SCRIPT></HEAD><BODY>
<table cellpadding="0" cellspacing="0" border="0" width="280" align="center">
<tr><td colspan="4">JavaScript检测密码安全强度等级代码</td></tr>
<tr><td colspan="4">
密码显示:<INPUT TYPE="text" NAME="pw2" class="texts" readonly>
数字:<span id="pwn"></span>字母:<span id="pwl"></span>
</td></tr>
<tr>
<td height="16" width="60" align="right">
输入密码:</td>
<td height="16" width="80">
<INPUT TYPE="password" NAME="pw" class="texts" maxlength="20" onkeypress="event.returnValue=OnlyNumberOrLetter()" onpropertychange="CkeckPasswordSalf('pw')"></td>
<td height="16" width="20"> </td>
<td height="16" width="120">
<table cellpadding="0" cellspacing="0" border="0" width="120" height="20">
<tr height="2">
<td id="s0"></td>
<td id="s1"></td>
<td id="s2"></td>
</tr>
<tr height="18">
<td id="p0" class="Init" align="center">弱</td>
<td id="p1" class="Init" align="center">中</td>
<td id="p2" class="Init" align="center">强</td>
</tr></table>
</td></tr></table>
</BODY></HTML>