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

开发 前端
使用Knockout更简单的来实现密码强度的验证。

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

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

原有代码请查看:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.     <title></title> 
  5. </head> 
  6. <body> 
  7.     <script type="text/javascript"> 
  8.         //CharMode函数   
  9. function CharMode(iN) {  
  10.             if (iN >=48&& iN <=57) //数字  
  11. return1;  
  12.             if (iN >=65&& iN <=90) //大写字母  
  13. return2;  
  14.             if (iN >=97&& iN <=122) //小写  
  15. return4;  
  16.             else  
  17.                 return8; //特殊字符   
  18.         }  
  19.  
  20.         //bitTotal函数   
  21. function bitTotal(num) {  
  22.             modes =0;  
  23.             for (i =0; i <4; i++) {  
  24.                 if (num &1) modes++;  
  25.                 num >>>=1;  
  26.             }  
  27.             return modes;  
  28.         }  
  29.  
  30.         //checkStrong函数   
  31. function checkStrong(sPW) {  
  32.             if (sPW.length <=4)  
  33.                 return0; //密码太短  
  34.             Modes =0;  
  35.             for (i =0; i < sPW.length; i++) {  
  36.                 Modes |= CharMode(sPW.charCodeAt(i));  
  37.             }  
  38.             return bitTotal(Modes);  
  39.         }  
  40.  
  41.  
  42.         //pwStrength函数   
  43. function pwStrength(pwd) {  
  44.             O_color ="#eeeeee";  
  45.             L_color ="#FF0000";  
  46.             M_color ="#FF9900";  
  47.             H_color ="#33CC00";  
  48.             if (pwd ==null|| pwd =='') {  
  49.                 Lcolor = Mcolor = Hcolor = O_color;  
  50.             } else {  
  51.                 S_level = checkStrong(pwd);  
  52.                 switch (S_level) {  
  53.                     case0:  
  54.                         Lcolor = Mcolor = Hcolor = O_color;  
  55.                     case1:  
  56.                         Lcolor = L_color;  
  57.                         Mcolor = Hcolor = O_color;  
  58.                         break;  
  59.                     case2:  
  60.                         Lcolor = Mcolor = M_color;  
  61.                         Hcolor = O_color;  
  62.                         break;  
  63.                     default:  
  64.                         Lcolor = Mcolor = Hcolor = H_color;  
  65.                 }  
  66.  
  67.                 document.getElementById("strength_L").style.background = Lcolor;  
  68.                 document.getElementById("strength_M").style.background = Mcolor;  
  69.                 document.getElementById("strength_H").style.background = Hcolor;  
  70.                 return;  
  71.             }  
  72.         } </script> 
  73.     <form name="form1" action=""> 
  74.     输入密码:<input type="password" size="10" onkeyup="pwStrength(this.value)" onblur="pwStrength(this.value)"> 
  75.     <br> 
  76.     密码强度:  
  77.     <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" 
  78.         height="23" style='display: inline'> 
  79.         <tr align="center" bgcolor="#eeeeee"> 
  80.             <td width="33%" id="strength_L"> 
  81.                 弱  
  82.             </td> 
  83.             <td width="33%" id="strength_M"> 
  84.                 中  
  85.             </td> 
  86.             <td width="33%" id="strength_H"> 
  87.                 强  
  88.             </td> 
  89.         </tr> 
  90.     </table> 
  91.     </form> 
  92. </body> 
  93. </html> 

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

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

然后来创建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.  
  5. viewModel.Lcolor = ko.dependentObservable(function () {  
  6.     //根据密码强度判断***个格显示的背景色  
  7.     return this.PasswordLevel() == 0 ? this.Ocolor : (this.PasswordLevel() == 1 ? "#FF0000" : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00"))  
  8. }, viewModel);  
  9.  
  10. viewModel.Mcolor = ko.dependentObservable(function () {  
  11.     //根据密码强度判断第二个格显示的背景色  
  12.     return this.PasswordLevel() < 2 ? this.Ocolor : (this.PasswordLevel() == 2 ? "#FF9900" : "#33CC00")  
  13. }, viewModel);  
  14.  
  15. viewModel.Hcolor = ko.dependentObservable(function () {  
  16.     //根据密码强度判断第三个格显示的背景色  
  17.     return this.PasswordLevel() < 3 ? this.Ocolor : "#33CC00"  
  18. }, viewModel); 

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

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

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

  1. <form name="form1" action=""> 
  2. 输入密码:  
  3. <input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'"> 
  4. <br> 
  5. 密码强度:  
  6. <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" 
  7.     height="23" style='display: inline'> 
  8.     <tr align="center" bgcolor="#eeeeee"> 
  9.         <td width="50"data-bind="style: { backgroundColor: Lcolor }"></td> 
  10.         <td width="50"data-bind="style: { backgroundColor: Mcolor }"></td> 
  11.         <td width="50"data-bind="style: { backgroundColor: Hcolor }"></td> 
  12.     </tr> 
  13. </table> 
  14. </form> 

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

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

 

完整版代码如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
  2. <html> 
  3. <head> 
  4.     <script type="text/javascript" src="http://knockoutjs.com/js/jquery-1.4.2.min.js"></script> 
  5.     <script type="text/javascript" src="http://knockoutjs.com/js/jquery.tmpl.js"></script> 
  6.     <script type="text/javascript" src="http://knockoutjs.com/js/knockout-1.2.1.js"></script> 
  7. </head> 
  8. <body> 
  9.     <script type="text/javascript"> 
  10.         var PagePage = Page || {};  
  11.         PagePage.Utility = Page.Utility || {};  
  12.         PagePage.Utility.Registration = Page.Utility.Registration || {};  
  13.  
  14.         //获取密码强度  
  15.         Page.Utility.Registration.getPasswordLevel =function (password) {  
  16.             if (password ==null|| password =='')  
  17.                 return0;  
  18.  
  19.             if (password.length <=4)  
  20.                 return0; //密码太短  
  21.  
  22.             var Modes =0;  
  23.             for (i =0; i < password.length; i++) {  
  24.                 Modes |= CharMode(password.charCodeAt(i));  
  25.             }  
  26.             return bitTotal(Modes);  
  27.  
  28.             //CharMode函数   
  29. function CharMode(iN) {  
  30.                 if (iN >=48&& iN <=57) //数字  
  31. return1;  
  32.                 if (iN >=65&& iN <=90) //大写字母  
  33. return2;  
  34.                 if (iN >=97&& iN <=122) //小写  
  35. return4;  
  36.                 else  
  37.                     return8; //特殊字符   
  38.             }  
  39.  
  40.             //bitTotal函数  
  41. function bitTotal(num) {  
  42.                 modes =0;  
  43.                 for (i =0; i <4; i++) {  
  44.                     if (num &1) modes++;  
  45.                     num >>>=1;  
  46.                 }  
  47.                 return modes;  
  48.             }  
  49.         };  
  50.  
  51.         var viewModel = {  
  52.             Password: ko.observable(""),  
  53.             Ocolor: "#eeeeee"  
  54.         };  
  55.  
  56.         viewModel.PasswordLevel = ko.dependentObservable(function () {  
  57.             return Page.Utility.Registration.getPasswordLevel(this.Password());  
  58.         }, viewModel);  
  59.  
  60.         viewModel.Lcolor = ko.dependentObservable(function () {  
  61.             //根据密码强度判断***个格显示的背景色  
  62. returnthis.PasswordLevel() ==0?this.Ocolor : (this.PasswordLevel() ==1?"#FF0000" : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00"))  
  63.         }, viewModel);  
  64.  
  65.         viewModel.Mcolor = ko.dependentObservable(function () {  
  66.             //根据密码强度判断第二个格显示的背景色  
  67. returnthis.PasswordLevel() <2?this.Ocolor : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00")  
  68.         }, viewModel);  
  69.  
  70.         viewModel.Hcolor = ko.dependentObservable(function () {  
  71.             //根据密码强度判断第二个格显示的背景色  
  72. returnthis.PasswordLevel() <3?this.Ocolor : "#33CC00"  
  73.         }, viewModel);  
  74.  
  75.         $((function () {  
  76.             ko.applyBindings(viewModel);  
  77.         }));  
  78.  
  79.          
  80.     </script> 
  81.     <form name="form1" action=""> 
  82.     输入密码:<input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'"> 
  83.     <br> 
  84.     密码强度:  
  85.     <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" 
  86.         height="23" style='display: inline'> 
  87.         <tr align="center" bgcolor="#eeeeee"> 
  88.             <td width="50" id="strength_L" data-bind="style: { backgroundColor: Lcolor }"> 
  89.                 弱  
  90.             </td> 
  91.             <td width="50" id="strength_M" data-bind="style: { backgroundColor: Mcolor }"> 
  92.                 中  
  93.             </td> 
  94.             <td width="50" id="strength_H" data-bind="style: { backgroundColor: Hcolor }"> 
  95.                 强  
  96.             </td> 
  97.         </tr> 
  98.     </table> 
  99.     </form> 
  100. </body> 
  101. </html> 

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

【系列文章】

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

 

责任编辑:陈贻新 来源: 汤姆大叔的博客
相关推荐

2020-06-07 11:46:05

密码信息泄露高强度密码

2019-07-02 13:16:05

密码账号安全数据安全

2023-01-03 08:17:04

JavaScript检测用户

2017-01-19 09:16:19

2022-04-23 16:36:30

Linux密码

2009-06-15 11:22:06

2020-07-03 07:00:00

Linux用户活动

2010-04-07 11:04:52

Oracle用户密码

2017-01-05 14:01:38

linux密码高强度

2019-03-18 09:00:04

Linux密码cracklib

2016-11-08 17:56:37

Linux命令行密码

2013-05-29 09:47:45

2010-04-08 18:21:56

Oracle用户密码

2010-04-19 17:10:53

Oracle用户密码

2010-10-29 09:13:33

Oracle用户密码

2014-03-14 09:45:18

2023-08-08 09:41:35

Windows微软

2013-01-06 13:45:14

2017-05-23 14:34:58

python大数据UUID

2010-05-28 18:58:05

MySQL 用户密码
点赞
收藏

51CTO技术栈公众号