2

我试图从 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')});

        });
4

3 回答 3

2

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

if(condition) thing();

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

if(condition) { thing(); }

为了消除任何歧义,如下所示:

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

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

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

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

您的程序很好并且运行良好,您决定 JSLint 验证的重要性。

于 2010-11-03T11:51:23.927 回答
0

这是“固定”代码。我删除了该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')});
});
于 2010-11-07T03:49:45.680 回答
0

在你写的代码中

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

在我看来,这

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

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

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

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

此外,我发现遵循关于全局变量$max、函数内部和添加声明的建议很好$num$temprandomPasswordvar

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

对于功能v和功能el内部也是$.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非常好,因为它有助于找到一些手动很难找到的小错误。

于 2010-11-03T12:29:02.670 回答