转(js密码强度)

<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>
<input name="password" type="password" id="password" size="20" onkeyup="return loadinputcontext(this);" />
<span id="showmsg"></span>
原文地址:https://www.cnblogs.com/suneryong/p/1301486.html