jquery验证错误预期的标识符,字符串或数字

时间:2010-09-23 21:33:58

标签: javascript jquery validation

我的jquery验证有点问题。在IE 6和IE 7中的我可能会收到一条错误消息:预期的标识符....

我最小化了我的代码,似乎,如果我删除了以下部分,那么一切都运行正常。 有什么问题?

我真的不明白额外的逗号在哪里,所以我决定粘贴孔验证脚本。请看一下我的代码。

jQuery.validator.addMethod("lettersonly", function(value, element) {
        return this.optional(element) || /^[a-zőöüóúéáűí ]+$/i.test(value);
}, "... betűket használjon");   

$("#test").validate({
    rules: {
        name: {
                required: true,
                minlength: 5,
                maxlength: 40,
                lettersonly: true
        },
        addr: {
                required: true,
                minlength: 15,
                maxlength: 80


        },
        phone: {
                required: true,
                minlength: 8,
                maxlength: 20,
                number: true         
        },
            email: {
                required: true,
                minlength: 5,
                email: true      
        },
        count: {
        required: true,
                minlength: 3,
                maxlength: 20,
                number: true         
        },
        nettoj: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        nettoj2: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        mini: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        mini2: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        adosj: {
                minlength: 3,
                maxlength: 20,
                number: true         
        },
        amini: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        stil: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        tcount: {
                minlength: 2,
                maxlength: 20,
                number: true         
        },
        city: {
                minlength: 3,
                maxlength: 60,
                lettersonly: true        
        },


    },
    messages: {
        name: {
                required: "... !",
                minlength: "Minimum 5 ",
                maxlength: "Maximum 40 "
        },
        addr: {
                required: "... ",
                minlength: "Minimum 15 ",
                maxlength: "Maximum 80 "
        },
        phone: {
                required: "... ",
                minlength: "Minimum 8 ",
                maxlength: "Maximum 20 ",
                number: "... "

        },

        email: {
                required: "... e-mail ",
                minlength: "Minimum 5 ",
                email: "..."

        },
        count: {
                required: "... ",
                minlength: "Minimum 3 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
            nettoj: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
            nettoj2: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                mini: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                mini2: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                adosj: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                amini: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
            number: "... "

        },
                stil: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                tcount: {
                minlength: "Minimum 2 ",
                maxlength: "Maximum 20 ",
                                number: "... "

        },
                city: {
                minlength: "Minimum 3 ",
                maxlength: "Maximum 80 "
        },

    },



   });  

2 个答案:

答案 0 :(得分:3)

这是因为你在你的对象文字属性中添加了一个额外的逗号,除掉它

object = {
    property1: 23,
    property2: 14,
    property3: 42 //The answer, also, no comma after the 42, as it's the last
}                 //property of the object, if you have a comma here, then the
                  //javascript engine is expecting another member item to the
                  //object, and get's really peeved if you don't add it.

最小化代码工作的原因是因为它非常好并且摆脱了那些令你讨厌的javascript引擎的令人讨厌的额外逗号。

修改

仍然在嵌套级别之间的新代码中有额外的逗号

    city: {                    //Look, it's the last member of an object
            minlength: 3,
            maxlength: 60,
            lettersonly: true        
    }, //<-- WHAT IS THAT COMMA DOING THERE!!!!!!!!!!!!!!!

/*At this point, parser is like, wtf, where's the next member?*/},

答案 1 :(得分:1)

删除此段代码中的最后一个,

addr: {
    required: "...",
    minlength: "...",
    maxlength: "..."
},

addr: {
    required: "...",
    minlength: "...",
    maxlength: "..."
}