【问题标题】:JSLint Shows so many errors but my program is working fineJSLint 显示很多错误,但我的程序运行良好
【发布时间】:2011-05-04 10:56:18
【问题描述】:

我试图从 JSLint 验证我的 JQuery 代码并得到很多错误。请让我知道如何解决。

这是下面的密码强度计代码

    $.fn.passwordstrength = function(options){
                    return this.each(function(){
                    var that = this;that.opts = {};
                    that.opts = $.extend({}, $.fn.passwordstrength.defaults, options);

                    that.div = $(that.opts.targetDiv);
                    that.defaultClass = that.div.attr('class');

                    that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;

                    v = $(this)
                    .keyup(function(){
                    if(typeof el == "undefined")
                    this.el = $(this);
                    var s = getPasswordStrength(this.value);
                    var p = this.percents;
                    var t = Math.floor(s/p);
                    if(100 <= s)
                        t = this.opts.classes.length - 1;
                    this.div
                        .removeAttr('class')
                        .addClass( this.defaultClass )
                        .addClass( this.opts.classes[ t ] );

                })
            });

            function getPasswordStrength(H){
                var D=(H.length);
                if (D<4){ 
                    D=0;
                }
                if(D>5){
                    D=5;
                }
                // This is patern for non-numeric characters
                var F=H.replace(/[0-9]/g,"");
                var G=(H.length-F.length);
                if(G>3){
                    G=3;
                }
                // This is patern for uppercase and lowercase evaluation 
                var A=H.replace(/\W/g,"");
                var C=(H.length-A.length);
                if(C>3){
                    C=3;
                }

                var B=H.replace(/[A-Z]/g,"");
                var I=(H.length-B.length);
                if(I>3){
                    I=3;
                }

                // This is patern for Special Characters
                var P=H.replace(/^[@#$^&]*$/,"");
                var Q=(H.length-P.length);
                if(Q>3){
                    Q=3;
                }

                var E=((D*10)-20)+(G*10)+(C*15)+(I*10)+(Q*10);
                if(E<0){
                    E=0;
                }
                if(E>100){
                    E=100;
                }
                return E;
            }

            function randomPassword() {
                var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$_+";
                var size = 10;
                var i = 1;
                var ret = "";
                while (i <= size) {
                    $max = chars.length-1;
                    $num = Math.floor(Math.random()*$max);
                    $temp = chars.substr($num, 1);
                    ret += $temp;
                    i++;
                }
                return ret;
            }

        };

        $(document)
        .ready(function(){
            $('input[name="password"]').passwordstrength({targetDiv: '#pwd_strength',classes : Array('weak','medium','strong','complex')});

        });

【问题讨论】:

    标签: jquery jslint


    【解决方案1】:

    您的错误是样式错误,JSLint 不喜欢这样:

    if(condition) thing();
    

    它想看到if(){},像这样:

    if(condition) { thing(); }
    

    为了消除歧义,像这样:

    if(condition)
    thing();
    otherThing();
    

    otherThing() 无论条件如何都会运行,但乍一看并不清楚,而这是:

    if(condition) {
      thing();
    }
    otherThing();
    

    它也不喜欢可选的分号(我也不喜欢,用它们该死的!),总是包含它们,这是纯粹的风格:它希望看到在顶部声明的内部函数getPasswordStrength它是父母。

    您的程序很好,可以正常运行,由您决定 JSLint 验证的重要性。

    【讨论】:

      【解决方案2】:

      在您编写的代码中

      if(typeof el == "undefined")
      this.el = $(this);
      

      在我看来

      if(typeof el == "undefined") {
          this.el = $(this);
      }
      

      阅读会更好(如果这是您想要的并且您不会忘记更多内容)。如果您以后决定为您的 JavaScript 代码使用一些最小化工具,那么“{”和“}”的使用对于接收始终正确的工作代码可能非常重要。

      在语句末尾写分号在 JavaScript 中是一种很好的风格,那么为什么不按照建议放在下一条语句之后呢?

      return this.each(function(){
         // ...
      })
      

      此外,我发现在 randomPassword 函数中遵循关于全局变量 $max$num$temp 的建议并添加 var 声明是很好的选择

      var $max = chars.length-1;
      var $num = Math.floor(Math.random()*$max);
      var $temp = chars.substr($num, 1);
      

      vel$.fn.passwordstrength 函数内部也是如此。如果你会使用

      var v = $(this)
      

      $(this)
      

      (因为您没有在代码中使用v。可能是您想使用this.v,但您应该像我一样了解这一点)而不是

      v = $(this)
      

      if(typeof this.el == "undefined") {
          this.el = $(this);
      }
      

      而不是

      if(typeof el == "undefined") {
          this.el = $(this);
      }
      

      由于使用了相同的全局变量,您的程序会运行得更快,并且潜在的冲突更少

      删除未使用的函数randomPassword 也是一个好主意。

      我个人觉得JSLint 非常好,因为它有助于找到一些手动很难找到的小错误。

      【讨论】:

      • 您好 Oleg,我需要使用以下标准编辑我的 Regx 和密码 attrength 代码。 1. 密码混合了字母和数字 2. 密码也混合了小写和大写字母 3. 密码也至少有 8 个字符或更多 4. 密码也有特殊字符,如 ?、*、\、“” 或 + 5 . 密码也至少有 10 个字符
      • @SASHI:对不起,Sashi,但是RegEx 是如何连接到您主要问题中的 JSLint 错误的?唯一的 JSLint 警告是建议将 /^[@#$^&amp;]*$/ 替换为 /^[@#$\^&amp;]*$/
      【解决方案3】:

      这是“固定”代码。我删除了 randomPassword 函数,因为您的代码中没有引用它。

      /*global $:false, document:false, el:true*/
      
      $.fn.passwordstrength = function(options){
          function getPasswordStrength(H){
              var D=(H.length);
              if (D<4){ 
                  D=0;
              }
              if(D>5){
                  D=5;
              }
              // This is patern for non-numeric characters
              var F=H.replace(/[0-9]/g,"");
              var G=(H.length-F.length);
              if(G>3){
                  G=3;
              }
              // This is patern for uppercase and lowercase evaluation 
              var A=H.replace(/\W/g,"");
              var C=(H.length-A.length);
              if(C>3){
                  C=3;
              }
      
              var B=H.replace(/[A-Z]/g,"");
              var I=(H.length-B.length);
              if(I>3){
                  I=3;
              }
      
              // This is patern for Special Characters
              var P=H.replace(/^[@#$\^&]*$/,"");
              var Q=(H.length-P.length);
              if(Q>3){
                  Q=3;
              }
      
              var E=((D*10)-20)+(G*10)+(C*15)+(I*10)+(Q*10);
              if(E<0){
                  E=0;
              }
              if(E>100){
                  E=100;
              }
              return E;
          }
      
          return this.each(function(){
              var that = this;that.opts = {};
              that.opts = $.extend({}, $.fn.passwordstrength.defaults, options);
      
              that.div = $(that.opts.targetDiv);
              that.defaultClass = that.div.attr('class');
      
              that.percents = (that.opts.classes.length) ? 100 / that.opts.classes.length : 100;
      
              $(this).keyup(function(){
                  if(typeof el == "undefined"){
                      this.el = $(this);
                  }
                  var s = getPasswordStrength(this.value);
                  var p = this.percents;
                  var t = Math.floor(s/p);
                  if(100 <= s){
                      t = this.opts.classes.length - 1;
                  }
                  this.div
                      .removeAttr('class')
                      .addClass( this.defaultClass )
                      .addClass( this.opts.classes[ t ] );
      
              });
          });
      };
      
      $(document).ready(function(){
          $('input[name="password"]').passwordstrength({targetDiv: '#pwd_strength',classes : Array('weak','medium','strong','complex')});
      });
      

      【讨论】:

      • @SASHI 如果我的回答对你有用,你可以vote upaccept 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多