专注于.Net

享受编程的乐趣
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

密码强度

Posted on 2008-07-24 11:49  古道飘零客  阅读(386)  评论(0编辑  收藏  举报
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>层测试</title>
<script type="text/javascript">
        var PasswordStrength ={
            Level : ["极佳","一般","较弱","太短"],//强度提示信息
            LevelValue : [15,10,5,0],//和上面提示信息对应的强度值
            Factor : [1,2,5],//强度加权数,分别为字母,数字,其它
            KindFactor : [0,0,10,20],//密码含几种组成的权数
            Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字符,数字,非字符数字(即特殊符号)
            }
        PasswordStrength.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;
        }
        PasswordStrength.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];
            }
        }
    function loadinputcontext(o)
    {
        var showmsg=PasswordStrength.StrengthLevel(o.value);
        switch(showmsg)
        {
        case "太短": showmsg+=" <img src='images/level/1.gif' width='88' height='11' />";
        break;
        case "较弱": showmsg+=" <img src='images/level/2.gif' width='88' height='11' />";
        break;
        case "一般": showmsg+=" <img src='images/level/3.gif' width='88' height='11' />";
        break;
        case "极佳": showmsg+=" <img src='images/level/4.gif' width='88' height='11' />";
        break;
         }
         document.getElementById('showmsg').innerHTML = showmsg;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <input name="password" type="password" id="password" size="20" onkeyup="return loadinputcontext(this);" />
        <span id="showmsg"></span>
    </form>
</body>
</html>