日期:2014-05-16  浏览次数:20511 次

[JS]验证密码强度
调用方法,在input加上onkeyup="ADKPasswordStrength.Check(this,'lb')";
ADKPasswordStrength.Check
方法说明
参数
iobj:输入框的ID或者OBJECT
sobj:显示信息的ID或者OBJECT

css定义3中CSS 分别是高,一般,弱!可以自己改CSS

[html]
<!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>无标题文档</title>
<style type="text/css">
.ADKPasswordStrength_low{ background-color:#CC0000;}
.ADKPasswordStrength_middle{ background-color:#33CCFF;}
.ADKPasswordStrength_height{ background-color:#66FF00;}
</style>
<script type="text/javascript">
var ADKPasswordStrength =
{
   Level : ["高","一般","弱"],
   LevelValue : [30,20,0],//强度值
   Factor : [1,2,5],//字符加数,分别为字母,数字,其它
   KindFactor : [0,0,10,20],//密码含几种组成的加数
   Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字符正则数字正则其它正则
}
ADKPasswordStrength.StrengthValue=function(pwd)
{
   var strengthValue = 0;
   var ComposedKind = 0;
   for(var i = 0 ; i < this.Regex.length;i++)
   {
       var chars = pwd.match(this.Regex[i]);
        if(chars != null)
        {
                strengthValue += chars.length * this.Factor[i];
                ComposedKind ++;
         }
     }
    strengthValue += this.KindFactor[ComposedKind];
    return strengthValue;
}
   
ADKPasswordStrength.StrengthLevel = function(pwd)
{
   var value = this.StrengthValue(pwd);
    for(var i = 0 ; i < this.LevelValue.length ; i ++)
    {
        if(value >= this.LevelValue[i] )
        return this.Level[i];
    }
}

ADKPasswordStrength.Check=function(iobj,sobj)
{
    var inputobj,showobj;
   
    inputobj=typeof(iobj)=="string"?document.getElementById(iobj):iobj;
    showobj=typeof(sobj)=="string"?document.getElementById(sobj):sobj;

    if(!showobj || !inputobj)
        return;

    var lv=ADKPasswordStrength.StrengthLevel(inputobj.value);
    if(lv=="高")
    {
        showobj.innerHTML="高";
        showobj.className="ADKPasswordStrength_height"
    }
    else if(lv=="一般")
    {
        showobj.innerHTML="一般";
        showobj.className="ADKPasswordStrength_middle"
    }
    else if(lv=="弱")
    {
        showobj.innerHTML="弱";
        showobj.className="ADKPasswordStrength_low"
    }
}
</script>
</head>
<body>
<input type="password" id="textpass" size="20" onkeyup="ADKPasswordStrength.Check(this,'lb');" /><div id="lb"></div>
</body&g