用Knockout评估用户输入密码的强度

我们来看看如果使用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. //bitTotal函数
  20. function bitTotal(num) {
  21. modes =0;
  22. for (i =0; i <4; i++) {
  23. if (num &1) modes++;
  24. num >>>=1;
  25. }
  26. return modes;
  27. }
  28. //checkStrong函数
  29. function checkStrong(sPW) {
  30. if (sPW.length <=4)
  31. return0; //密码太短
  32. Modes =0;
  33. for (i =0; i < sPW.length; i++) {
  34. Modes |= CharMode(sPW.charCodeAt(i));
  35. }
  36. return bitTotal(Modes);
  37. }
  38. //pwStrength函数
  39. function pwStrength(pwd) {
  40. O_color ="#eeeeee";
  41. L_color ="#FF0000";
  42. M_color ="#FF9900";
  43. H_color ="#33CC00";
  44. if (pwd ==null|| pwd =='') {
  45. Lcolor = Mcolor = Hcolor = O_color;
  46. } else {
  47. S_level = checkStrong(pwd);
  48. switch (S_level) {
  49. case0:
  50. Lcolor = Mcolor = Hcolor = O_color;
  51. case1:
  52. Lcolor = L_color;
  53. Mcolor = Hcolor = O_color;
  54. break;
  55. case2:
  56. Lcolor = Mcolor = M_color;
  57. Hcolor = O_color;
  58. break;
  59. default:
  60. Lcolor = Mcolor = Hcolor = H_color;
  61. }
  62. document.getElementById("strength_L").style.background = Lcolor;
  63. document.getElementById("strength_M").style.background = Mcolor;
  64. document.getElementById("strength_H").style.background = Hcolor;
  65. return;
  66. }
  67. } </script>
  68. <form name="form1" action="">
  69. 输入密码:<input type="password" size="10" onkeyup="pwStrength(this.value)" onblur="pwStrength(this.value)">
  70. <br>
  71. 密码强度:
  72. <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
  73. height="23" style='display: inline'>
  74. <tr align="center" bgcolor="#eeeeee">
  75. <td width="33%" id="strength_L">
  76. </td>
  77. <td width="33%" id="strength_M">
  78. </td>
  79. <td width="33%" id="strength_H">
  80. </td>
  81. </tr>
  82. </table>
  83. </form>
  84. </body>
  85. </html>

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

 
 
  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. <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. Page.Utility.Registration.getPasswordLevel =function (password) {
  15. if (password ==null|| password =='')
  16. return0;
  17. if (password.length <=4)
  18. return0; //密码太短
  19. var Modes =0;
  20. for (i =0; i < password.length; i++) {
  21. Modes |= CharMode(password.charCodeAt(i));
  22. }
  23. return bitTotal(Modes);
  24. //CharMode函数
  25. function CharMode(iN) {
  26. if (iN >=48&& iN <=57) //数字
  27. return1;
  28. if (iN >=65&& iN <=90) //大写字母
  29. return2;
  30. if (iN >=97&& iN <=122) //小写
  31. return4;
  32. else
  33. return8; //特殊字符
  34. }
  35. //bitTotal函数
  36. function bitTotal(num) {
  37. modes =0;
  38. for (i =0; i <4; i++) {
  39. if (num &1) modes++;
  40. num >>>=1;
  41. }
  42. return modes;
  43. }
  44. };
  45. var viewModel = {
  46. Password: ko.observable(""),
  47. Ocolor: "#eeeeee"
  48. };
  49. viewModel.PasswordLevel = ko.dependentObservable(function () {
  50. return Page.Utility.Registration.getPasswordLevel(this.Password());
  51. }, viewModel);
  52. viewModel.Lcolor = ko.dependentObservable(function () {
  53. //根据密码强度判断第一个格显示的背景色
  54. returnthis.PasswordLevel() ==0?this.Ocolor : (this.PasswordLevel() ==1?"#FF0000" : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00"))
  55. }, viewModel);
  56. viewModel.Mcolor = ko.dependentObservable(function () {
  57. //根据密码强度判断第二个格显示的背景色
  58. returnthis.PasswordLevel() <2?this.Ocolor : (this.PasswordLevel() ==2?"#FF9900" : "#33CC00")
  59. }, viewModel);
  60. viewModel.Hcolor = ko.dependentObservable(function () {
  61. //根据密码强度判断第二个格显示的背景色
  62. returnthis.PasswordLevel() <3?this.Ocolor : "#33CC00"
  63. }, viewModel);
  64. $((function () {
  65. ko.applyBindings(viewModel);
  66. }));
  67. </script>
  68. <form name="form1" action="">
  69. 输入密码:<input type="text" size="10" data-bind="value:Password, valueUpdate: 'afterkeydown'">
  70. <br>
  71. 密码强度:
  72. <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc"
  73. height="23" style='display: inline'>
  74. <tr align="center" bgcolor="#eeeeee">
  75. <td width="50" id="strength_L" data-bind="style: { backgroundColor: Lcolor }">
  76. </td>
  77. <td width="50" id="strength_M" data-bind="style: { backgroundColor: Mcolor }">
  78. </td>
  79. <td width="50" id="strength_H" data-bind="style: { backgroundColor: Hcolor }">
  80. </td>
  81. </tr>
  82. </table>
  83. </form>
  84. </body>
  85. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值