AS3教程:密码强度验证

作者:网络 来源:佚名 更新时间:2009-08-09 17:26:23 点击:

public static function evaluatepwd(spw:string):int
{
if (spw.length <= 4)
return 0;
var modes:int = 0;
for (var i:int = 0; i < spw.length; i++)
{
modes |= charmode(spw.charcodeat(i));
}

return bittotal(modes);

function charmode(in:int):int
{
if (in>=48 && in <=57)
return 1;
if (in>=65 && in <=90)
return 2;
if (in>=97 && in <=122)
return 4;
else
return 8;
}

function bittotal(num:int):*
{
var modes:int = 0;
for (var i:int = 0; i < 4; i++)
{
if (num & 1) modes ++;
num >>>= 1;
}
return modes;
}
}

public static function evaluatepwd(spw:string):int
  {
   if (spw.length <= 4)
    return 0;
   var modes:int = 0;
   for (var i:int = 0; i < spw.length; i++)
   {
    modes |= charmode(spw.charcodeat(i));
   }

   return bittotal(modes);

   function charmode(in:int):int
   {
    if (in>=48 && in <=57)
     return 1;
    if (in>=65 && in <=90)
     return 2;
    if (in>=97 && in <=122)
     return 4;
    else
     return 8;
   }

   function bittotal(num:int):*
   {
    var modes:int = 0;
    for (var i:int = 0; i < 4; i++)
    {
     if (num & 1) modes ++;
     num >>>= 1;
    }
    return modes;
   }
  }另外还有一个十分简单的算法

public static function evaluatepwd2(spw:string):int
{
return spw.replace(/^(?:([a-z])|([a-z])|([0-9])|(.)){5,}|(.)+$/g, “$1$2$3$4$5′).length;
}
public static function evaluatepwd2(spw:string):int
  {
   return spw.replace(/^(?:([a-z])|([a-z])|([0-9])|(.)){5,}|(.)+$/g, "$1$2$3$4$5").length;
  }

我们可以用多种图形化的界面甚至动画去展现密码的强度,举个简单的例子,例如我希望用户在输入密码后1秒内没有任何输入动作,则验证密码的强度,并且已进度条的形式展示

首先注册一个监听

pwdinputview.password.addeventlistener(keyboardevent.key_up, onkey);
pwdinputview.password.addeventlistener(keyboardevent.key_up, onkey);然后看看监听函数

private var oldtext:string;
private function onkey(e:keyboardevent):void
{
if (oldtext != pwdinputview.password.text && pwdinputview.password.text.length > 5)
{
oldtext = pwdinputview.password.text;
tweenlite.killtweensof(updatestrengthview, false);
tweenlite.delayedcall(1, updatestrengthview);
}
}
private var oldtext:string;

  private function onkey(e:keyboardevent):void
  {
   if (oldtext != pwdinputview.password.text && pwdinputview.password.text.length > 5)
   {
    oldtext = pwdinputview.password.text;
    tweenlite.killtweensof(updatestrengthview, false);
    tweenlite.delayedcall(1, updatestrengthview);
   }
  }

首先定义一个oldtext成员,用于记录上次输入的密码,如果当前密码和上次密码不相同并且长度大于5,开始执行操作。我这里用了tweenlite作为计时器,1秒后将执行强度判断,如果发现用户在1秒内再次有输入操作,则销毁当前tweenlite。

再看看如何用视图来展示密码强度,这里不多作解释了,只是一个简单的动画效果。

private function updatestrengthview():void
{
var towidth:number = pwdinputview.strengthcanvas.width * checkstrong.evaluatepwd(pwdinputview.password.text) * .25;
tweenlite.to(pwdinputview.maskcanvas, 1.5, {width:towidth, ease:bounce.easeout, overwrite:false});
}
private function updatestrengthview():void
  {
   var towidth:number = pwdinputview.strengthcanvas.width * checkstrong.evaluatepwd(pwdinputview.password.text) * .25;
   tweenlite.to(pwdinputview.maskcanvas, 1.5, {width:towidth, ease:bounce.easeout, overwrite:false});
  }