jQuery validate across multiple fields

后端 未结 3 1432
天涯浪人
天涯浪人 2020-12-14 11:54

I have 3 input fields that ask for the number of people, and of them how many are adults and how many are children. I am trying to use jQuery validate plugin to add a custom

3条回答
  •  别那么骄傲
    2020-12-14 12:07

    As suggested in comments this is not a good practice. Nonetheless, if you still want to do it:

    $("#form2").validate({
        errorElement: "span",
            rules: {
            attendees: {
                 equal: $("#form2 #adults").val() + $("#form2 #children").val();
            },              
            adults: {
                 required: true,
                 digits: true
            },              
            children: {
                 required: true,
                 digits: true
            }
        },
        messages: {
            attendees: "Enter the number of persons (including yourself)", 
            adults: "Enter number of adults", 
            children: "Enter number of childern"                
        }               
    });
    

提交回复
热议问题