jQuery Validation插件:禁用指定提交按钮的验证 [英] jQuery Validation plugin: disable validation for specified submit buttons

查看:116
本文介绍了jQuery Validation插件:禁用指定提交按钮的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含多个要通过JörnZaeffere出色的jQuery Validation插件进行验证的字段(有些还添加了用于自定义验证的方法).您如何规避使用指定提交控件的验证(换句话说,使用某些提交输入进行验证,而不使用其他提交输入进行验证)?这类似于带有标准ASP.NET验证程序控件的ValidationGroups.

I have a form with multiple fields that I'm validating (some with methods added for custom validation) with Jörn Zaeffere's excellent jQuery Validation plugin. How do you circumvent validation with specified submit controls (in other words, fire validation with some submit inputs, but do not fire validation with others)? This would be similar to ValidationGroups with standard ASP.NET validator controls.

我的情况:

它与ASP.NET WebForms一起使用,但是如果您愿意,可以忽略它.但是,我将验证更多地用作建议":换句话说,提交表单时,验证会触发,但不是显示必填"消息,而是建议"显示的内容与您"相似错过了以下领域....您是否仍要继续?"此时,在错误容器中,现在可以看到另一个提交按钮,可以按下该按钮,它将忽略验证并仍然提交.如何规避此按钮控件的表单.validate()并仍然发布?

It's with ASP.NET WebForms, but you can ignore that if you wish. However, I am using the validation more as a "recommendation": in other words, when the form is submitted, validation fires but instead of a "required" message displaying, a "recommendation" shows that says something along the line of "you missed the following fields.... do you wish to proceed anyways?" At that point in the error container there's another submit button now visible that can be pressed which would ignore the validation and submit anyways. How to circumvent the forms .validate() for this button control and still post?

" http://jquery.bassistance.de/validate上的房屋买卖示例/demo/multipart/允许这样做,以命中先前的链接,但这是通过创建自定义方法并将其添加到验证程序来实现的.我希望不必创建自定义方法来复制验证插件中已经存在的功能.

The Buy and Sell a House sample at http://jquery.bassistance.de/validate/demo/multipart/ allows for this in order to hit the previous links, but it does so through creating custom methods and adding it to the validator. I would prefer to not have to create custom methods duplicating functionality already in the validation plugin.

以下是我现在可以立即使用的脚本的简化版本:

The following is a shortened version of the immediately applicable script that I've got right now:

var container = $("#<%= Form.ClientID %> div.validationSuggestion");

$('#<%= Form.ClientID %>').validate({          
    errorContainer: container,
    errorLabelContainer: $("ul",container),
    rules: {
        <%= YesNo.UniqueID %>: { required: true },
        <%= ShortText.UniqueID %>: { required: true } // etc.

    },
    messages: {
        <%= YesNo.UniqueID %>: 'A message.',
        <%= ShortText.UniqueID %>: 'Another message.' // etc.
    },
    highlight: function(element, errorClass) {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").removeClass("valid");
    },
    unhighlight: function(element, errorClass) {
        $(element).removeClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass("valid");
    },
    wrapper: 'li'
}); 

推荐答案

您可以将cancel的CSS类添加到提交按钮以禁止验证

You can add a CSS class of cancel to a submit button to suppress the validation

例如

<input class="cancel" type="submit" value="Save" />

在此处查看此功能的jQuery Validator文档:在提交时跳过验证

See the jQuery Validator documentation of this feature here: Skipping validation on submit

编辑:

不建议使用上述技术,并已将其替换为formnovalidate属性.

The above technique has been deprecated and replaced with the formnovalidate attribute.

<input formnovalidate="formnovalidate" type="submit" value="Save" />

这篇关于jQuery Validation插件:禁用指定提交按钮的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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