jQuery验证组 [英] Jquery validation groups

查看:70
本文介绍了jQuery验证组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道jquery验证组如何工作,也不知道它们应该如何工作.

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?

在jquery验证文档中找不到与之相关的任何信息...

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>

您仍然必须为这些字段(在这种情况下为必填项)及其自定义消息定义单独的规则.唯一的区别是,如果其中一个或所有验证均未通过,则会输出一条消息.据我所知,组名称,例如:'nameGroup'在组功能之外不可用.

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.

希望这会有所帮助.

这篇关于jQuery验证组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆