验证具有相同输入字段名称的表单?

时间:2013-08-20 14:25:58

标签: javascript jquery html

我有以下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

但我相信不需要}}。

我在这里遗漏了什么吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

你在这里错过了一个逗号:

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

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

答案 1 :(得分: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"
        }
    } );            
});