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

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

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

原有代码请查看:

 
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <scripttype="text/javascript">
  8. //CharMode函数
  9. functionCharMode(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. functionbitTotal(num){
  21. modes=0;
  22. for(i=0;i<4;i++){
  23. if(num&1)modes++;
  24. num>>>=1;
  25. }
  26. returnmodes;
  27. }
  28. //checkStrong函数
  29. functioncheckStrong(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. returnbitTotal(Modes);
  37. }
  38. //pwStrength函数
  39. functionpwStrength(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. <formname="form1"action="">
  69. 输入密码:<inputtype="password"size="10"onkeyup="pwStrength(this.value)"onblur="pwStrength(this.value)">
  70. <br>
  71. 密码强度:
  72. <tablewidth="217"border="1"cellspacing="0"cellpadding="1"足球平台出租 class="attribute">bordercolor="#cccccc"
  73. height="23"style='display:inline'>
  74. <tralign="center"bgcolor="#eeeeee">
  75. <tdwidth="33%"id="strength_L">
  76. </td>
  77. <tdwidth="33%"id="strength_M">
  78. </td>
  79. <tdwidth="33%"id="strength_H">
  80. </td>
  81. </tr>
  82. </table>
  83. </form>
  84. </body>
  85. </html>

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

 
  1. varPagePage=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. return0;
  8. if(password.length<=4)
  9. return0;//密码太短
  10. varModes=0;
  11. for(i=0;i<password.length;i++){
  12. Modes|=CharMode(password.charCodeAt(i));
  13. }
  14. returnbitTotal(Modes);
  15. //CharMode函数
  16. functionCharMode(iN){
  17. if(iN>=48&&iN<=57)//数字
  18. return1;
  19. if(iN>=65&&iN<=90)//大写字母
  20. return2;
  21. if(iN>=97&&iN<=122)//小写
  22. return4;
  23. else
  24. return8;//特殊字符
  25. }
  26. //bitTotal函数
  27. functionbitTotal(num){
  28. modes=0;
  29. for(i=0;i<4;i++){
  30. if(num&1)modes++;
  31. num>>>=1;
  32. }
  33. returnmodes;
  34. }
  35. };

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

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

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

 
  1. viewModel.PasswordLevel=ko.dependentObservable(function(){
  2. returnPage.Utility.Registration.getPasswordLevel(this.Password());
  3. },viewModel);
  4. viewModel.Lcolor=ko.dependentObservable(function(){
  5. //根据密码强度判断第一个格显示的背景色
  6. returnthis.PasswordLevel()==0 this.Ocolor:(this.PasswordLevel()==1?"#FF0000":(this.PasswordLevel()==2?"#FF9900":"#33CC00"))
  7. },viewModel);
  8. viewModel.Mcolor=ko.dependentObservable(function(){
  9. //根据密码强度判断第二个格显示的背景色
  10. returnthis.PasswordLevel()<2?this.Ocolor:(this.PasswordLevel()==2?"#FF9900":"#33CC00")
  11. },viewModel);
  12. viewModel.Hcolor=ko.dependentObservable(function(){
  13. //根据密码强度判断第三个格显示的背景色
  14. returnthis.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. <formname=足球平台出租pan class="attribute-value">"form1"action="">
  2. 输入密码:
  3. <inputtype="text"size="10"data-bind="value:Password,valueUpdate:'afterkeydown'">
  4. <br>
  5. 密码强度:
  6. <tablewidth="217"border="1"cellspacing="0"cellpadding="1"bordercolor="#cccccc"
  7. height="23"style='display:inline'>
  8. <tralign="center"bgcolor="#eeeeee">
  9. <tdwidth="50"data-bind="style:{backgroundColor:Lcolor}"></td>
  10. <tdwidth="50"data-bind="style:{backgroundColor:Mcolor}"></td>
  11. <tdwidth="50"data-bind="style:{backgroundColor:Hcolor}"></td>
  12. </tr>
  13. </table>
  14. </form>

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

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

完整版代码如下:

 
  1. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <scripttype="text/javascript"src="http://knockoutjs.com/js/jquery-1.4.2.min.js"></script>
  5. <scripttype="text/javascript"src="http://knockoutjs.com/js/jquery.tmpl.js"></script>
  6. <scripttype="text/javascript"src="http://knockoutjs.com/js/knockout-1.2.1.js"></script>
  7. </head>
  8. <body>
  9. <scripttype="text/javascript">
  10. varPagePage=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. varModes=0;
  20. for(i=0;i<password.length;i++){
  21. Modes|=CharMode(password.charCodeAt(i));
  22. }
  23. returnbitTotal(Modes);
  24. //CharMode函数
  25. functionCharMode(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. functionbitTotal(num){
  37. modes=0;
  38. for(i=0;i<4;i++){
  39. if(num&1)modes++;
  40. num>>>=1;
  41. }
  42. returnmodes;
  43. }
  44. };
  45. varviewModel={
  46. Password:ko.observable(""),
  47. Ocolor:"#eeeeee"
  48. };
  49. viewModel.PasswordLevel=ko.dependentObservable(function(){
  50. returnPage.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. <formname="form1"action="">
  69. 输入密码:<inputtype="text"size="10"data-bind="value:Password,valueUpdate:'afterkeydown'">
  70. <br>
  71. 密码强度:
  72. <tablewidth="217"border="1"cellspacing="0"cellpadding="1"bordercolor="#cccccc"
  73. height="23"style='display:inline'>
  74. <tralign="center"bgcolor="#eeeeee">
  75. <tdwidth="50"id="strength_L"data-bind="style:{backgroundColor:Lcolor}">
  76. </td>
  77. <tdwidth="50"id="strength_M"data-bind="style:{backgroundColor:Mcolor}">
  78. </td>
  79. <tdwidth="50"id="strength_H"data-bind="style:{backgroundColor:Hcolor}">
  80. </td>
  81. </tr>
  82. </table>
  83. </form>
  84. </body>
  85. </html>

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

【系列文章】

您可能感兴趣的文章:
发布一个JavaScript工具类库jutil_0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值