博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

检测密码强度的javascript

Posted on 2007-08-28 16:02  孤峰皓月  阅读(394)  评论(0编辑  收藏  举报
 1<script type="text/javascript">
 2        var PasswordStrength ={
 3            Level : ["高,实在是高","还行啦","靠,这样也行"],
 4            LevelValue : [30,20,0],//强度值
 5            Factor : [1,2,5],//字符加数,分别为字母,数字,其它
 6            KindFactor : [0,0,10,20],//密码含几种组成的加数 
 7            Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字符正则数字正则其它正则
 8            }

 9        PasswordStrength.StrengthValue = function(pwd)
10        {
11            var strengthValue = 0;
12            var ComposedKind = 0;
13            for(var i = 0 ; i < this.Regex.length;i++)
14            {
15                var chars = pwd.match(this.Regex[i]);
16                if(chars != null)
17                {
18                    strengthValue += chars.length * this.Factor[i];
19                    ComposedKind ++;
20                }

21            }

22            strengthValue += this.KindFactor[ComposedKind];
23            return strengthValue;
24        }
 
25        PasswordStrength.StrengthLevel = function(pwd)
26        {
27            var value = this.StrengthValue(pwd);
28            for(var i = 0 ; i < this.LevelValue.length ; i ++)
29            {
30                if(value >= this.LevelValue[i] )
31                    return this.Level[i];
32            }

33        }

34       
35        alert(PasswordStrength.StrengthLevel("23")); 
36        alert(PasswordStrength.StrengthLevel("abcd123"));     
37        alert(PasswordStrength.StrengthLevel("abcd!%23"));  
38    </script>
39