-3

我有下面的 jquery 代码来验证表单。

function validateForm(){


        $("input.field1").each(function(){
        $(this).rules("add", {
            required: true,
            messages: {
                required: "Required"
            }
        } );            
    });



    $("input.fieldTwo").each(function(){
        $(this).rules("add", {
            required: true,
            maxlength: 12,
            email: true
            messages: {
                required: "Enter email",
                email: "Enter valid email",
                maxlength: "Maximum 12 characters"
            }
        } );            
    });



    $("input.field3").each(function(){
        $(this).rules("add", {
            required: false,
            maxlength: 12
            messages: {
                maxlength: "Maximum 12 characters"
            }
        } );            
    });

    $("input.field4").each(function(){
        $(this).rules("add", {
            required: false,
            maxlength: 12
            messages: {
                maxlength: "Maximum 12 characters"
            }
        } );            
    });

    $("input.field5").each(function(){
        $(this).rules("add", {
            required: false,
            maxlength: 12
            messages: {
                maxlength: "Maximum 12 characters"
            }
        } );            
    });



        return $("#myForm").validate({
          onfocusout: function(element) { jQuery(element).valid(); } 
    });

  }

但它总是给出脚本错误说SyntaxError: missing } after property list

但我相信没有 } 是必需的。

我在这里错过了什么吗?

谢谢!

4

2 回答 2

1

您在这里缺少一个逗号:

 $("input.field3").each(function(){
        $(this).rules("add", {
            required: false,
            maxlength: 12, // added a comma here
            messages: {
                maxlength: "Maximum 12 characters"
            }
        } );            
    });

maxlength实际上,您在属性之后的每个区域都错过了一个逗号。可能是复制和粘贴错误?

于 2013-08-20T14:28:25.877 回答
0

您缺少几个逗号。查看代码,并在整个过程中进行复制。

$("input.fieldTwo").each(function(){
    $(this).rules("add", {
        required: true,
        maxlength: 12,
        email: true  //MISSING COMMA
        messages: {
            required: "Enter email",
            email: "Enter valid email",
            maxlength: "Maximum 12 characters"
        }
    } );            
});

$("input.field3").each(function(){
    $(this).rules("add", {
        required: false,
        maxlength: 12  //MISSING COMMA
        messages: {
            maxlength: "Maximum 12 characters"
        }
    } );            
});

$("input.field4").each(function(){
    $(this).rules("add", {
        required: false,
        maxlength: 12  //MISSING COMMA
        messages: {
            maxlength: "Maximum 12 characters"
        }
    } );            
});
于 2013-08-20T14:31:02.193 回答