密码强度检测

 

密码强度检测”这个功能挺实用,对用户输入的密码进行复杂程度的评估。所以就从网上找了很多类似的,我这边进行整理一下,写了一个PasswordStrength类

 

passwordstrength.js

// 密码强度;
function  PasswordStrength(showed)
{    
    
this .showed  =  ( typeof (showed)  ==   " boolean " ) ? showed: true ;
    
this .styles  =   new  Array();    
    
this .styles[ 0 =  {backgroundColor: " #EBEBEB " ,borderLeft: " solid 1px #FFFFFF " ,borderRight: " solid 1px #BEBEBE " ,borderBottom: " solid 1px #BEBEBE " };    
    
this .styles[ 1 =  {backgroundColor: " #FF4545 " ,borderLeft: " solid 1px #FFFFFF " ,borderRight: " solid 1px #BB2B2B " ,borderBottom: " solid 1px #BB2B2B " };
    
this .styles[ 2 =  {backgroundColor: " #FFD35E " ,borderLeft: " solid 1px #FFFFFF " ,borderRight: " solid 1px #E9AE10 " ,borderBottom: " solid 1px #E9AE10 " };
    
this .styles[ 3 =  {backgroundColor: " #95EB81 " ,borderLeft: " solid 1px #FFFFFF " ,borderRight: " solid 1px #3BBC1B " ,borderBottom: " solid 1px #3BBC1B " };
    
this .labels =  [ " " , " " , " " ];
    
this .divName  =   " pwd_div_ " + Math.ceil(Math.random() * 100000 );
    
this .minLen  =   1 ; //     
     this .width  =   " 200px " ;
    
this .height  =   " 16px " ;    
    
this .content  =   "" ;    
    
this .selectedIndex  =   0 ;    
    
this .init();    
}
PasswordStrength.prototype.init 
=   function ()
{
    
var  s  =   ' <table cellpadding="0" id=" ' + this .divName + ' _table" cellspacing="0" style="width: ' + this .width + ' ;height: ' + this .height + ' ;"> ' ;
    s 
+=   ' <tr> ' ;
    
for ( var  i = 0 ;i < 3 ;i ++ ){
        s 
+=   ' <td id=" ' + this .divName + ' _td_ ' + i + ' " width="33%" align="center"><span style="font-size:1px">&nbsp;</span><span id=" ' + this .divName + ' _label_ ' + i + ' " style="display:none;font-family: Courier New, Courier, mono;font-size: 12px;color: #000000;"> ' + this .labels[i] + ' </span></td> ' ;
    }    
    s 
+=   ' </tr> ' ;
    s 
+=   ' </table> ' ;
    
this .content  =  s;
    
if ( this .showed)
    {
        document.write(s);
        
this .copyToStyle( this .selectedIndex);
    }    
}
PasswordStrength.prototype.copyToObject 
=   function (o1,o2)
{
    
for ( var  i  in  o1)
    {
        o2[i] 
=  o1[i];
    }
}
PasswordStrength.prototype.copyToStyle 
=   function (id)
{
    
this .selectedIndex  =  id;
    
for ( var  i = 0 ;i < 3 ;i ++ )
    {
        
if (i  ==  id - 1 )
        {
            
this .$( this .divName + " _label_ " + i).style.display  =   " inline " ;
        }
        
else
        {
            
this .$( this .divName + " _label_ " + i).style.display  =   " none " ;
        }
    }
    
for ( var  i = 0 ;i < id;i ++ )
    {
        
this .copyToObject( this .styles[id], this .$( this .divName + " _td_ " + i).style);            
    }
    
for (;i < 3 ;i ++ )
    {
        
this .copyToObject( this .styles[ 0 ], this .$( this .divName + " _td_ " + i).style);
    }
}
PasswordStrength.prototype.$ 
=   function (s)
{
    
return  document.getElementById(s);
}
PasswordStrength.prototype.setSize 
=   function (w,h)
{
    
this .width  =  w;
    
this .height  =  h;
}
PasswordStrength.prototype.setMinLength 
=   function (n)
{
    
if (isNaN(n))
    {
        
return  ;
    }
    n 
=  Number(n);
    
if (n > 1 ){
        
this .minLength  =  n;
    }
}
PasswordStrength.prototype.setStyles 
=   function ()
{
    
if (arguments.length  ==   0 )
    {
        
return  ;
    }
    
for ( var  i = 0 ;i < arguments.length  &&  i  <   4 ;i ++ )
    {
        
this .styles[i]  =  arguments[i];
    }
    
this .copyToStyle( this .selectedIndex);
}
PasswordStrength.prototype.write 
=   function (s)
{
    
if ( this .showed)
    {
        
return  ;
    }
    
var  n  =  (s  ==   ' string ' ?   this .$(s) : s;
    
if ( typeof (n)  !=   " object " )
    {
        
return  ;
    }
    n.innerHTML 
=   this .content;
    
this .copyToStyle( this .selectedIndex);
}
PasswordStrength.prototype.update 
=   function (s)
{
    
if (s.length  <   this .minLen)
    {
        
this .copyToStyle( 0 );
        
return ;
    }
    
var  ls  =   - 1 ;
    
if  (s.match( / [a - z] / ig))
    {
        ls
++ ;
    }
    
if  (s.match( / [ 0 - 9 ] / ig))
    {
        ls
++ ;
    }
     
if  (s.match( / (.[ ^ a - z0 - 9 ]) / ig))
     {
        ls
++ ;
    }
    
if  (s.length  <   6   &&  ls  >   0 )
    {
        ls
-- ;
    }
     
switch (ls) 
     { 
         
case   0 :
             
this .copyToStyle( 1 );
             
break ;
         
case   1 :
             
this .copyToStyle( 2 );
             
break ;
         
case   2 :
             
this .copyToStyle( 3 );
             
break ;
         
default :
             
this .copyToStyle( 0 );
     }
}

 

 调用

< html >
< head >
< title > PasswordStrength </ title >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8" >
< script  language ="javascript"  src ="js/passwordstrength.js" ></ script >
</ head >
< body >  
< h4 > 密码强度检测 </ h4 >
< table  width ="100%"   border ="0"  cellspacing ="1"  cellpadding ="0" >
  
< tr >
    
< td  width ="100"  align ="right" > 强度显示: </ td >
    
< td >
    
< script  language ="javascript" > var  ps = new  PasswordStrength(); </ script >
    
</ td >
  
</ tr >
  
< tr >
    
< td  align ="right" > 密码检测: </ td >
    
< td >< input  name ="pwd"  type ="password"  id ="pwd"  style ="width:200px"  onKeyUp ="ps.update(this.value);" ></ td >
  
</ tr >
</ table >
</ body >
</ html >

 

代码示例下载:http://www.cnblogs.com/Files/yangcai/passwordstrength.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的jQuery密码强度检测代码: ```javascript $(document).ready(function(){ $('#password').keyup(function(){ var password = $(this).val(); var strength = 0; if (password.match(/[a-z]+/)) { strength += 1; } if (password.match(/[A-Z]+/)) { strength += 1; } if (password.match(/[0-9]+/)) { strength += 1; } if (password.match(/[$@#&!]+/)) { strength += 1; } if (password.length >= 8) { strength += 1; } switch(strength) { case 0: $('#password-strength').html("密码强度:太弱了").css('color', 'red'); break; case 1: $('#password-strength').html("密码强度:弱").css('color', 'orange'); break; case 2: $('#password-strength').html("密码强度:一般").css('color', 'yellow'); break; case 3: $('#password-strength').html("密码强度").css('color', 'green'); break; case 4: $('#password-strength').html("密码强度:很").css('color', 'darkgreen'); break; default: $('#password-strength').html(""); } }); }); ``` 在这个代码中,我们使用了jQuery的`keyup()`方法来检测密码输入框的内容是否发生了变化。然后我们获取密码的值,并使用正则表达式来检查密码是否包含小写字母、大写字母、数字和特殊字符以及密码的长度是否大于等于8个字符。每个条件都会增加密码度值。最后,我们使用`switch()`语句根据不同的度值来显示不同的提示信息,并根据度值来设置不同的颜色。 你可以根据自己的需求来自定义密码强度检测的条件和提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值