Jquery validation groups

后端 未结 1 1234
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 05:05

I can\'t figure out how jquery validation groups work, nor how they should work.

I assumed it would serve as to validate conditions that needed more than

相关标签:
1条回答
  • 2020-12-15 05:15

    The main purpose I have used groups for is to produce one error message for a group of inputs and their respective validation conditions.

    For example if you want someones full name including title, first, and last name:

    <script type="text/javascript">
    $('#yourform').validate({
        //...Your valid logic...
        groups: {
            nameGroup: "title firstName lastName"
        },
        rules: {
            title: "required",
            firstName: "required",
            lastName: "required"
        },
        messages: {
            title: "Full name is required",
            firstName: "Full name is required",
            lastName: "Full name is required"
        } 
    });
    </script>
    
    <form id="yourform">
         <div>
            <input type="text" id="title" name="title" />              
            <input type="text" id="firstName" name="firstName" />              
            <input type="text" id="lastName" name="lastName" />   
        </div>
    </form>
    

    You still have to define the individual rules for these fields, in this case required and its custom message. The only difference being that if one or all of them failed validation it outputs one message. As far as I know the group name, eg: 'nameGroup' is not usable outside of the group function.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题