用JavaScript评估用户输入密码的强度(Knockout版)

早上看到博友6点多发的一篇关于密码强度的文章(连接),甚是感动(周末大早上还来发文)。

我们来看看如果使用Knockout更简单的来实现密码强度的验证。

原有代码请查看:

 
 
 
  1.     
  2.     
  3.         //CharMode函数 
  4. function CharMode(iN) {
  5.             if (iN >=48&& iN <=57) //数字
  6. return1;
  7.             if (iN >=65&& iN <=90) //大写字母
  8. return2;
  9.             if (iN >=97&& iN <=122) //小写
  10. return4;
  11.             else
  12.                 return8; //特殊字符 
  13.         }
  14.         //bitTotal函数 
  15. function bitTotal(num) {
  16.             modes =0;
  17.             for (i =0; i <4; i++) {
  18.                 if (num &1) modes++;
  19.                 num >>>=1;
  20.             }
  21.             return modes;
  22.         }
  23.         //checkStrong函数 
  24. function checkStrong(sPW) {
  25.             if (sPW.length <=4)
  26.                 return0; //密码太短
  27.             Modes =0;
  28.             for (i =0; i < sPW.length; i++) {
  29.                 Modes |= CharMode(sPW.charCodeAt(i));
  30.             }
  31.             return bitTotal(Modes);
  32.         }
  33.         //pwStrength函数 
  34. function pwStrength(pwd) {
  35.             O_color ="#eeeeee";
  36.             L_color ="#FF0000";
  37.             M_color ="#FF9900";
  38.             H_color ="#33CC00";
  39.             if (pwd ==null|| pwd =='') {
  40.                 Lcolor = Mcolor = Hcolor = O_color;
  41.             } else {
  42.                 S_level = checkStrong(pwd);
  43.                 switch (S_level) {
  44.                     case0:
  45.                         Lcolor = Mcolor = Hcolor = O_color;
  46.                     case1:
  47.                         Lcolor = L_color;
  48.                         Mcolor = Hcolor = O_color;
  49.                         break;
  50.                     case2:
  51.                         Lcolor = Mcolor = M_color;
  52.                         Hcolor = O_color;
  53.                         break;
  54.                     default:
  55.                         Lcolor = Mcolor = Hcolor = H_color;
  56.                 }
  57.                 document.getElementById("strength_L").style.background = Lcolor;
  58.                 document.getElementById("strength_M").style.background = Mcolor;
  59.                 document.getElementById("strength_H").style.background = Hcolor;
  60.                 return;
  61.             }
  62.         } 
  63.     
  64.     输入密码:
  65.     
  66.     密码强度:
  67.     
  68.         height="23" style='display: inline'>
  69.         
  70.             
  71.                 弱
  72.             
  73.             
  74.                 中
  75.             
  76.             
  77.                 强
  78.             
  79.         
  80.     
  81.     

首先我们来改善一下上面博友的验证函数为如下代码:

 
 
 
  1. var PagePage = Page || {};
  2. PagePage.Utility = Page.Utility || {};
  3. PagePage.Utility.Registration = Page.Utility.Registration || {};
  4. //获取密码强度
  5. Page.Utility.Registration.getPasswordLevel = function (password) {
  6.     if (password == null || password == '')
  7.         return 0;
  8.     if (password.length <= 4)
  9.         return 0; //密码太短
  10.     var Modes = 0;
  11.     for (i = 0; i < password.length; i++) {
  12.         Modes |= CharMode(password.charCodeAt(i));
  13.     }
  14.     return bitTotal(Modes);
  15.     //CharMode函数 
  16.     function CharMode(iN) {
  17.         if (iN >= 48 && iN <= 57) //数字
  18.             return 1;
  19.         if (iN >= 65 && iN <= 90) //大写字母
  20.             return 2;
  21.         if (iN >= 97 && iN <= 122) //小写
  22.             return 4;
  23.         else
  24.             return 8; //特殊字符 
  25.     }
  26.     //bitTotal函数
  27.     function bitTotal(num) {
  28.         modes = 0;
  29.         for (i = 0; i < 4; i++) {
  30.             if (num & 1) modes++;
  31.             num >>>= 1;
  32.         }
  33.         return modes;
  34.     }
  35. };

然后来创建View Model,但是引用Knockout之前,我们首先要引用Knockout的Js类库(具体介绍请查看Knockout应用开发指南的系列教程)
View model代码如下:

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

对于密码强度以及颜色的值依赖于密码字符串的值,所以我们需要为他们声明依赖属性,代码如下:

 
 
 
  1. viewModel.PasswordLevel = ko.dependentObservable(function () {
  2.     return Page.Utility.Registration.getPasswordLevel(this.Password());
  3. }, viewModel);
  4. viewModel.Lcolor = ko.dependentObservable(function () {
  5.     //根据密码强度判断***个格显示的背景色
  6.     return this.PasswordLevel() == 0 ? this.Ocolor : (this.PasswordLevel() == 1 ? "#FF0000" : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00"))
  7. }, viewModel);
  8. viewModel.Mcolor = ko.dependentObservable(function () {
  9.     //根据密码强度判断第二个格显示的背景色
  10.     return this.PasswordLevel() < 2 ? this.Ocolor : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00")
  11. }, viewModel);
  12. viewModel.Hcolor = ko.dependentObservable(function () {
  13.     //根据密码强度判断第三个格显示的背景色
  14.     return this.PasswordLevel() < 3 ? this.Ocolor : "#33CC00"
  15. }, viewModel);

然后使用applyBindings方法将view model绑定到该页面,你可以使用jQuery的ready函数来执行该绑定代码,也可以在页面最下方执行绑定代码,我们这里使用了jQuery,代码如下:

 
 
 
  1. $((function () {
  2.     ko.applyBindings(viewModel);
  3. }));

***,我们再看看这些值怎么动态绑定到HTML元素上的,请查看如下代码(其中使用了afterkeydown代替了onKeyUp和onBlur):

 
 
 
  1. 输入密码:

  2. 密码强度:
  3.     height="23" style='display: inline'>
  4.     
  5.         
  6.         
  7.         
  8.     

然后就OK,运行代码查看,一模一样的功能展示出来了。

如果去掉为验证而改善的代码,总代码肯定是比原有的方式少的。

完整版代码如下:

 
 
 
  1.     
  2.     
  3.     
  4.     
  5.         var PagePage = Page || {};
  6.         PagePage.Utility = Page.Utility || {};
  7.         PagePage.Utility.Registration = Page.Utility.Registration || {};
  8.         //获取密码强度
  9.         Page.Utility.Registration.getPasswordLevel =function (password) {
  10.             if (password ==null|| password =='')
  11.                 return0;
  12.             if (password.length <=4)
  13.                 return0; //密码太短
  14.             var Modes =0;
  15.             for (i =0; i < password.length; i++) {
  16.                 Modes |= CharMode(password.charCodeAt(i));
  17.             }
  18.             return bitTotal(Modes);
  19.             //CharMode函数 
  20. function CharMode(iN) {
  21.                 if (iN >=48&& iN <=57) //数字
  22. return1;
  23.                 if (iN >=65&& iN <=90) //大写字母
  24. return2;
  25.                 if (iN >=97&& iN <=122) //小写
  26. return4;
  27.                 else
  28.                     return8; //特殊字符 
  29.             }
  30.             //bitTotal函数
  31. function bitTotal(num) {
  32.                 modes =0;
  33.                 for (i =0; i <4; i++) {
  34.                     if (num &1) modes++;
  35.                     num >>>=1;
  36.                 }
  37.                 return modes;
  38.             }
  39.         };
  40.         var viewModel = {
  41.             Password: ko.observable(""),
  42.             Ocolor: "#eeeeee"
  43.         };
  44.         viewModel.PasswordLevel = ko.dependentObservable(function () {
  45.             return Page.Utility.Registration.getPasswordLevel(this.Password());
  46.         }, viewModel);
  47.         viewModel.Lcolor = ko.dependentObservable(function () {
  48.             //根据密码强度判断***个格显示的背景色
  49. returnthis.PasswordLevel() ==0?this.Ocolor : (this.PasswordLevel() ==1?"#FF0000" : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00"))
  50.         }, viewModel);
  51.         viewModel.Mcolor = ko.dependentObservable(function () {
  52.             //根据密码强度判断第二个格显示的背景色
  53. returnthis.PasswordLevel() <2?this.Ocolor : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00")
  54.         }, viewModel);
  55.         viewModel.Hcolor = ko.dependentObservable(function () {
  56.             //根据密码强度判断第二个格显示的背景色
  57. returnthis.PasswordLevel() <3?this.Ocolor : "#33CC00"
  58.         }, viewModel);
  59.         $((function () {
  60.             ko.applyBindings(viewModel);
  61.         }));
  62.        
  63.     
  64.     
  65.     输入密码:
  66.     
  67.     密码强度:
  68.     
  69.         height="23" style='display: inline'>
  70.         
  71.             
  72.                 弱
  73.             
  74.             
  75.                 中
  76.             
  77.             
  78.                 强
  79.             
  80.         
  81.     
  82.     

原文:http://www.cnblogs.com/TomXu/archive/2011/11/27/2264876.html

【系列文章】

  1. Knockout应用开发指南之创建自定义绑定
  2. Knockout应用开发指南之入门介绍
  3. Knockout应用开发指南之监控属性(Observables)
  4. Knockout应用开发指南之绑定语法
  5. Knockout应用开发指南之模板绑定

分享题目:用JavaScript评估用户输入密码的强度(Knockout版)
本文来源:http://www.shufengxianlan.com/qtweb/news40/367740.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联