AS3密碼強度驗證

來源:互聯網
上載者:User

AS3的密碼強度驗證函式

 
  1. public static function evaluatePwd(sPW:String):int
  2.         {
  3.             if (sPW.length <= 4)
  4.                 return 0;
  5.             var Modes:int = 0;
  6.             for (var i:int = 0; i < sPW.length; i++)
  7.             {
  8.                 Modes |= CharMode(sPW.charCodeAt(i));
  9.             }
  10.             
  11.             return bitTotal(Modes);
  12.             
  13.             function CharMode(iN:int):int
  14.             {
  15.                 if (iN>=48 && iN <=57)
  16.                     return 1;
  17.                 if (iN>=65 && iN <=90)
  18.                     return 2;
  19.                 if (iN>=97 && iN <=122)
  20.                     return 4;
  21.                 else
  22.                     return 8;
  23.             }
  24.             
  25.             function bitTotal(num:int):*
  26.             {
  27.                 var modes:int = 0;
  28.                 for (var i:int = 0; i < 4; i++)
  29.                 {
  30.                     if (num & 1) modes ++;
  31.                     num >>>= 1;
  32.                 }
  33.                 return modes;
  34.             }
  35.         }
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;elsereturn 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;}}

另外還有一個十分簡單的演算法

 
  1. public static function evaluatePwd2(sPW:String):int
  2.         {
  3.             return sPW.replace(/^(?:([a-z])|([A-Z])|([0-9])|(.)){5,}|(.)+$/g, "$1$2$3$4$5").length;
  4.         }
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秒內沒有任何輸入動作,則驗證密碼的強度,並且已進度條的形式展示

首先註冊一個監聽

 
  1. pwdInputView.password.addEventListener(KeyboardEvent.KEY_UP, onKey);
pwdInputView.password.addEventListener(KeyboardEvent.KEY_UP, onKey);

然後看看監聽函數

 
  1. private var oldText:String;
  2.  
  3. private function onKey(e:KeyboardEvent):void
  4. {
  5.     if (oldText != pwdInputView.password.text && pwdInputView.password.text.length > 5)
  6.     {
  7.         oldText = pwdInputView.password.text;
  8.         TweenLite.killTweensOf(updateStrengthView, false);
  9.         TweenLite.delayedCall(1, updateStrengthView);
  10.     }
  11. }
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。

再看看如何用視圖來展示密碼強度,這裡不多作解釋了,只是一個簡單的動畫效果。

 
  1. private function updateStrengthView():void
  2. {
  3.     var toWidth:Number = pwdInputView.strengthCanvas.width * CheckStrong.evaluatePwd(pwdInputView.password.text) * .25;
  4.     TweenLite.to(pwdInputView.maskCanvas, 1.5, {width:toWidth, ease:Bounce.easeOut, overwrite:false});
  5. }
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});}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.