AS3密码强度验证

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});
		}

http://blog.youmila.com/?p=24

你可能感兴趣的:(算法,Blog,UP)