<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      代碼改變世界

      用Javascript評估用戶輸入密碼的強(qiáng)度(Knockout版)

      2011-11-27 10:40  湯姆大叔  閱讀(15023)  評論(10)    收藏  舉報(bào)

      早上看到博友6點(diǎn)多發(fā)的一篇關(guān)于密碼強(qiáng)度的文章(連接),甚是感動(dòng)(周末大早上還來發(fā)文)。

      我們來看看如果使用Knockout更簡單的來實(shí)現(xiàn)密碼強(qiáng)度的驗(yàn)證。

      原有代碼請查看:

      View Code
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <title></title>
      </head>
      <body>
      <script type="text/javascript">
      //CharMode函數(shù)
      function CharMode(iN) {
      if (iN >=48&& iN <=57) //數(shù)字
      return1;
      if (iN >=65&& iN <=90) //大寫字母
      return2;
      if (iN >=97&& iN <=122) //小寫
      return4;
      else
      return8; //特殊字符
      }

      //bitTotal函數(shù)
      function bitTotal(num) {
      modes
      =0;
      for (i =0; i <4; i++) {
      if (num &1) modes++;
      num
      >>>=1;
      }
      return modes;
      }

      //checkStrong函數(shù)
      function checkStrong(sPW) {
      if (sPW.length <=4)
      return0; //密碼太短
      Modes =0;
      for (i =0; i < sPW.length; i++) {
      Modes
      |= CharMode(sPW.charCodeAt(i));
      }
      return bitTotal(Modes);
      }


      //pwStrength函數(shù)
      function pwStrength(pwd) {
      O_color
      ="#eeeeee";
      L_color
      ="#FF0000";
      M_color
      ="#FF9900";
      H_color
      ="#33CC00";
      if (pwd ==null|| pwd =='') {
      Lcolor
      = Mcolor = Hcolor = O_color;
      }
      else {
      S_level
      = checkStrong(pwd);
      switch (S_level) {
      case0:
      Lcolor
      = Mcolor = Hcolor = O_color;
      case1:
      Lcolor
      = L_color;
      Mcolor
      = Hcolor = O_color;
      break;
      case2:
      Lcolor
      = Mcolor = M_color;
      Hcolor
      = O_color;
      break;
      default:
      Lcolor
      = Mcolor = Hcolor = H_color;
      }

      document.getElementById(
      "strength_L").style.background = Lcolor;
      document.getElementById(
      "strength_M").style.background = Mcolor;
      document.getElementById(
      "strength_H").style.background = Hcolor;
      return;
      }
      }
      </script>
      <form name="form1" action="">
      輸入密碼:<input type="password" size="10" onkeyup="pwStrength(this.value)" onblur="pwStrength(this.value)">
      <br>
      密碼強(qiáng)度:
      <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
      height
      ="23" style='display: inline'>
      <tr align="center" bgcolor="#eeeeee">
      <td width="33%" id="strength_L">

      </td>
      <td width="33%" id="strength_M">

      </td>
      <td width="33%" id="strength_H">
      強(qiáng)
      </td>
      </tr>
      </table>
      </form>
      </body>
      </html>

       

      首先我們來改善一下上面博友的驗(yàn)證函數(shù)為如下代碼:

      var Page = Page || {};
      Page.Utility = Page.Utility || {};
      Page.Utility.Registration = Page.Utility.Registration || {};

      //獲取密碼強(qiáng)度
      Page.Utility.Registration.getPasswordLevel = function (password) {
      if (password == null || password == '')
      return 0;

      if (password.length <= 4)
      return 0; //密碼太短

      var Modes = 0;
      for (i = 0; i < password.length; i++) {
      Modes |= CharMode(password.charCodeAt(i));
      }
      return bitTotal(Modes);

      //CharMode函數(shù)
      function CharMode(iN) {
      if (iN >= 48 && iN <= 57) //數(shù)字
      return 1;
      if (iN >= 65 && iN <= 90) //大寫字母
      return 2;
      if (iN >= 97 && iN <= 122) //小寫
      return 4;
      else
      return 8; //特殊字符
      }

      //bitTotal函數(shù)
      function bitTotal(num) {
      modes = 0;
      for (i = 0; i < 4; i++) {
      if (num & 1) modes++;
      num >>>= 1;
      }
      return modes;
      }
      };

       

      然后來創(chuàng)建View Model,但是引用Knockout之前,我們首先要引用Knockout的Js類庫(具體介紹請查看Knockout應(yīng)用開發(fā)指南的系列教程)
      View model代碼如下:

      var viewModel = {
      Password: ko.observable(""),
      Ocolor: "#eeeeee"
      };

      對于密碼強(qiáng)度以及顏色的值依賴于密碼字符串的值,所以我們需要為他們聲明依賴屬性,代碼如下:

      viewModel.PasswordLevel = ko.dependentObservable(function () {
      return Page.Utility.Registration.getPasswordLevel(this.Password());
      }, viewModel);

      viewModel.Lcolor = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第一個(gè)格顯示的背景色
      return this.PasswordLevel() == 0 ? this.Ocolor : (this.PasswordLevel() == 1 ? "#FF0000" : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00"))
      }, viewModel);

      viewModel.Mcolor = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第二個(gè)格顯示的背景色
      return this.PasswordLevel() < 2 ? this.Ocolor : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00")
      }, viewModel);

      viewModel.Hcolor = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第三個(gè)格顯示的背景色
      return this.PasswordLevel() < 3 ? this.Ocolor : "#33CC00"
      }, viewModel);

      然后使用applyBindings方法將view model綁定到該頁面,你可以使用jQuery的ready函數(shù)來執(zhí)行該綁定代碼,也可以在頁面最下方執(zhí)行綁定代碼,我們這里使用了jQuery,代碼如下:

      $((function () {
      ko.applyBindings(viewModel);
      }));

       

      最后,我們再看看這些值怎么動(dòng)態(tài)綁定到HTML元素上的,請查看如下代碼(其中使用了afterkeydown代替了onKeyUp和onBlur):

      <form name="form1" action="">
      輸入密碼:
      <
      input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'">
      <br>
      密碼強(qiáng)度:
      <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
      height
      ="23" style='display: inline'>
      <tr align="center" bgcolor="#eeeeee">
      <td width="50"data-bind="style: { backgroundColor: Lcolor }"></td>
      <td width="50"data-bind="style: { backgroundColor: Mcolor }"></td>
      <td width="50"data-bind="style: { backgroundColor: Hcolor }">強(qiáng)</td>
      </tr>
      </table>
      </form>

      然后就OK,運(yùn)行代碼查看,一模一樣的功能展示出來了。

      如果去掉為驗(yàn)證而改善的代碼,總代碼肯定是比原有的方式少的。

       

      完整版代碼如下:

      View Code
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
      <html>
      <head>
      <script type="text/javascript" src="http://knockoutjs.com/js/jquery-1.4.2.min.js"></script>
      <script type="text/javascript" src="http://knockoutjs.com/js/jquery.tmpl.js"></script>
      <script type="text/javascript" src="http://knockoutjs.com/js/knockout-1.2.1.js"></script>
      </head>
      <body>
      <script type="text/javascript">
      var Page = Page || {};
      Page.Utility
      = Page.Utility || {};
      Page.Utility.Registration
      = Page.Utility.Registration || {};

      //獲取密碼強(qiáng)度
      Page.Utility.Registration.getPasswordLevel =function (password) {
      if (password ==null|| password =='')
      return0;

      if (password.length <=4)
      return0; //密碼太短

      var Modes =0;
      for (i =0; i < password.length; i++) {
      Modes
      |= CharMode(password.charCodeAt(i));
      }
      return bitTotal(Modes);

      //CharMode函數(shù)
      function CharMode(iN) {
      if (iN >=48&& iN <=57) //數(shù)字
      return1;
      if (iN >=65&& iN <=90) //大寫字母
      return2;
      if (iN >=97&& iN <=122) //小寫
      return4;
      else
      return8; //特殊字符
      }

      //bitTotal函數(shù)
      function bitTotal(num) {
      modes
      =0;
      for (i =0; i <4; i++) {
      if (num &1) modes++;
      num
      >>>=1;
      }
      return modes;
      }
      };

      var viewModel = {
      Password: ko.observable(
      ""),
      Ocolor:
      "#eeeeee"
      };

      viewModel.PasswordLevel
      = ko.dependentObservable(function () {
      return Page.Utility.Registration.getPasswordLevel(this.Password());
      }, viewModel);

      viewModel.Lcolor
      = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第一個(gè)格顯示的背景色
      returnthis.PasswordLevel() ==0?this.Ocolor : (this.PasswordLevel() ==1?"#FF0000" : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00"))
      }, viewModel);

      viewModel.Mcolor
      = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第二個(gè)格顯示的背景色
      returnthis.PasswordLevel() <2?this.Ocolor : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00")
      }, viewModel);

      viewModel.Hcolor
      = ko.dependentObservable(function () {
      //根據(jù)密碼強(qiáng)度判斷第二個(gè)格顯示的背景色
      returnthis.PasswordLevel() <3?this.Ocolor : "#33CC00"
      }, viewModel);

      $((
      function () {
      ko.applyBindings(viewModel);
      }));


      </script>
      <form name="form1" action="">
      輸入密碼:<input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'">
      <br>
      密碼強(qiáng)度:
      <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
      height
      ="23" style='display: inline'>
      <tr align="center" bgcolor="#eeeeee">
      <td width="50" id="strength_L" data-bind="style: { backgroundColor: Lcolor }">

      </td>
      <td width="50" id="strength_M" data-bind="style: { backgroundColor: Mcolor }">

      </td>
      <td width="50" id="strength_H" data-bind="style: { backgroundColor: Hcolor }">
      強(qiáng)
      </td>
      </tr>
      </table>
      </form>
      </body>
      </html>
      主站蜘蛛池模板: 欧美日韩国产图片区一区| 国产精品黄色精品黄色大片| 日本国产精品第一页久久| 真人性囗交视频| 成人aⅴ综合视频国产| 野花香视频在线观看免费高清版 | 蜜臀精品一区二区三区四区| 亚洲天堂精品一区二区| 久久无码高潮喷水| 真人性囗交视频| 一亚洲一区二区中文字幕| 亚洲人午夜精品射精日韩| 少妇又紧又色又爽又刺激视频| 中文字幕亚洲精品人妻| 午夜福利电影| 国产精品一二三区蜜臀av| 露脸国产精品自产拍在线观看| 色偷偷www.8888在线观看| 在线精品另类自拍视频| 国精偷拍一区二区三区| 国产太嫩了在线观看| 国产精品亚洲а∨天堂2021| 高清无码18| 精品 日韩 国产 欧美 视频| 欧美视频网站www色| 亚洲国产精品日韩av专区| 国产综合精品一区二区三区| 成人免费无遮挡无码黄漫视频| 亚洲精品二区在线播放| 久久亚洲精品中文字幕无| 狠狠色噜噜狠狠狠狠777米奇| 国产中文字幕精品在线| 泗洪县| 久久人搡人人玩人妻精品| 色窝窝免费播放视频在线| 国产午夜精品久久久久免费视| 国产av亚洲精品ai换脸电影| 亚洲成人av一区二区| 久久精品国产热久久精品国产亚洲| 中文字幕av无码免费一区| 99在线小视频|