开发者

Jquery validation groups

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 one element to be tested, is that it?

Couldn't find anything about it on the jquery validation docs...


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜