jQuery 验证插件:以编程方式验证页面上某些部分的字段

发布于 2024-11-09 06:31:40 字数 1328 浏览 0 评论 0原文

我正在尝试使用 jQuery 验证插件(从此处 -->http://bassistance.de/jquery-plugins/jquery-plugin-validation/)以编程方式验证某些部分中的字段在进入新部分之前,基于按钮单击的页面(在最后部分结束之前不是真正的表单提交,它将验证整个表单)。本质上,当您单击特定部分中的按钮时,该部分中的字段需要验证。页面的第一部分验证得很好,但是当我到达第二部分时,它只是继续进行而不进行验证。执行此操作的正确方法是什么?我至少走在正确的轨道上吗?
这是我正在谈论的内容的一个示例。如果需要的话我可以添加更多代码。

// Step 1 section validation
var validateStep1 = $("#form1").validate({
    errorClass: "warning",
    onkeyup: false,
    onblur: false,
    onfocusout: true,
    rules: {
        "field1": {
            required: true,
        },
        "field2": {
            required: true,
            minlength: 1
        },
        "field3": {
            required: true,
        }
    }
});

// Step 2 section validation
var validateStep2 = $("#form1").validate({
    errorClass: "warning",
    onkeyup: false,
    onblur: false,
    onfocusout: true,
    rules: {
        "field4": {
            required: true,
        },
        "field5": {
            required: true,

        },
        "field6": {
            required: true,
        }
    }
});

// click events for buttons to proceed
$("#button1").click( function() {
    if (validateStep1.form()) {
        // proceed
    }
});
$("#button2").click( function() {
    if (validateStep2.form()) {
        // proceed
    }
});

预先感谢您的帮助!

I'm trying to use the jQuery validation plugin (from here -->http://bassistance.de/jquery-plugins/jquery-plugin-validation/) to programmatically validate fields in certain sections of a page based on a button click (not a true form submission until the end the final section where it will validate the entire form) prior to proceeding to a new section. Essentially when you click a button in a particular section, the fields in that section need to validate. The first section of the page validates just fine, but when I get to the second section, it just proceeds right through without validating. What is the proper way to do this? Am I at least on the right track??
here's a sample of what i'm talking about. I can add more code if necessary.

// Step 1 section validation
var validateStep1 = $("#form1").validate({
    errorClass: "warning",
    onkeyup: false,
    onblur: false,
    onfocusout: true,
    rules: {
        "field1": {
            required: true,
        },
        "field2": {
            required: true,
            minlength: 1
        },
        "field3": {
            required: true,
        }
    }
});

// Step 2 section validation
var validateStep2 = $("#form1").validate({
    errorClass: "warning",
    onkeyup: false,
    onblur: false,
    onfocusout: true,
    rules: {
        "field4": {
            required: true,
        },
        "field5": {
            required: true,

        },
        "field6": {
            required: true,
        }
    }
});

// click events for buttons to proceed
$("#button1").click( function() {
    if (validateStep1.form()) {
        // proceed
    }
});
$("#button2").click( function() {
    if (validateStep2.form()) {
        // proceed
    }
});

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-16 06:31:40

我想您不能在同一表单上使用两次不同的“验证”方法。

您可以使用规则对象上的“取决于”选项。它允许仅根据“取决于”的结果来“应用”规则。

此页面上有一个示例: http://rocketsquared.com/wiki/Plugins/Validation /validate#toptions(转到“规则”文档部分)

$(".selector").validate({
    rules: {
        contact: {
            required: true,
            email: {
                depends: function(element) {
                    return $("#contactform_email:checked")
                }
            }
        }
    }
});

希望这有帮助,
d.

I guess you cannot use two times differently the 'validate' method on the same form.

What you could use is the "depends" option on the rules objects. It allows to 'apply' the rule only based on the result of the "depends".

There is an example on this page: http://rocketsquared.com/wiki/Plugins/Validation/validate#toptions (go to the 'rules' documentation section)

$(".selector").validate({
    rules: {
        contact: {
            required: true,
            email: {
                depends: function(element) {
                    return $("#contactform_email:checked")
                }
            }
        }
    }
});

Hope this helps,
d.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文