jQuery Validate:验证特定组 [英] jQuery Validate : Validating specific group

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

问题描述

我正在尝试使用jQuery Validate,我的要求之一是验证某些特定的组,使其通过向导用户界面.不过,我似乎无法找到此功能的jQuery Validate文档,只能在网上找到非常模糊的参考.

I am attempting to use jQuery Validate, and one of my requirements is to validate some specific groups as they go through a wizard user interface. I cannot seem to locate the jQuery Validate documentation for this feature, though, and can only find very vague references to it online.

我的声明本质上是这样的

My declaration is essentially like this;

form.validate({
                    groups: {
                        raceGroup: "race",
                        identityGroup: "name gender age"
                    },
                    rules: {
                        race: {
                            required: true,
                        },
                        gender: {
                            valueNotEquals: "Select Gender ...",
                            required: true
                        },
                        name: {
                            pattern: "^(?!.*[ ]{2})(?!.*[']{2})(?!.*[-]{2})(?:[a-zA-Z0-9 \p{L}'-]{3,64}$)$"
                        }
                    },
                    messages: {
                        race: {
                            required: "this is a required for your character."
                        },
                        name: {
                            pattern: "You have entered an invalid name."
                        },
                        gender: {
                            valueNotEquals: "You must select a valid gender.",
                            required: "You must select a valid gender."
                        }
                    }
                });

好吧,所以我已经定义了组...但是现在呢?如何检查组中的所有内容是否有效? (或无效)

Okay, so I have the groups defined ... but now what? How can I check if everything within a group is valid? (or invalid)

推荐答案

groups: {
    raceGroup: "race",
    identityGroup: "name gender age"
},

如何检查组中的所有内容是否有效?(或无效)"

你不知道. groups选项不是这样的.

You don't. This is not how the groups option works.

使用groups选项,您只是将几条错误消息归为一组.例如:如果您有一组字段,每个字段都使用require_from_group规则(该组中必须是 any 一个或多个"字段),则groups选项将确保只有一个消息出现,而不是在每个输入旁边重复显示.

Using the groups option, you are merely grouping together several error messages into one. Example: if you have a group of fields, each using the require_from_group rule (any 'one or more' fields out of the group are required), the groups option will ensure that only one message appears instead of repeated next to every input.

没有用于创建验证组的已定义选项或标准化设置.

There is no defined option or standardized setup for creating validation groups.

如果您要制作阶梯式表格,可以使用多种方法.

If you're trying to do a stepped form, there are various approaches.

当我创建多步骤表单时,我为每个部分使用一组唯一的<form>标签.然后,我使用 .valid()方法来测试该部分,然后再继续进行下一部分. (别忘了先初始化插件;在DOM上的所有表单上都调用.validate().)

When I create multi-step forms, I use a unique set of <form> tags for each section. Then I use the .valid() method to test the section before moving to the next. (Don't forget to first initialize the plugin; call .validate(), on all forms on DOM ready.)

然后在最后一节中,我在每种表单上使用.serialize()并将它们连接到要提交的数据查询字符串中.

Then on the last section, I use .serialize() on each form and concatenate them into a data query string to be submitted.

类似这样的事情...

Something like this...

$(document).ready(function() {

    $('#form1').validate({ // initialize form 1
        // rules
    });

    $('#gotoStep2').on('click', function() { // go to step 2
        if ($('#form1').valid()) {
            // code to reveal step 2 and hide step 1
        }
    });

    $('#form2').validate({ // initialize form 2
        // rules
    });

    $('#gotoStep3').on('click', function() { // go to step 3
        if ($('#form2').valid()) {
            // code to reveal step 3 and hide step 2
        }
    });

    $('#form3').validate({ // initialize form 3
        // rules,
        submitHandler: function (form) {
           // serialize and join data for all forms
           // ajax submit
           return false;
        }
    });

    // there is no third click handler since the plugin takes care of this with the
    // built-in submitHandler callback function on the last form.

});

重要的是要记住我上面的click处理程序没有使用type="submit"按钮.这些是常规按钮,它们是form标签的外部type="button".

Important to remember that my click handlers above are not using type="submit" buttons. These are regular buttons, either outside of the form tags or type="button".

仅最后一个表单上的按钮是常规的type="submit"按钮.那是因为我仅在最后一种形式上利用了插件的内置submitHandler回调函数.

Only the button on the very last form is a regular type="submit" button. That is because I am leveraging the plugin's built-in submitHandler callback function on only the very last form.

概念验证"演示: http://jsfiddle.net/N9UpD/

"Proof of Concept" DEMO: http://jsfiddle.net/N9UpD/

另外,请参阅以供参考:

Also, see for reference:

https://stackoverflow.com/a/17975061/594235

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

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