jQuery 禁用单个字段的规则验证

发布于 2024-09-02 02:17:14 字数 506 浏览 1 评论 0原文

我正在使用 MVC 创建在运行时生成的表单。对于验证,我正在尝试使用 jQuery 验证库,它使用起来非常方便。我在标签的 cdata 属性中有每个字段的验证表达式,

<input type="text" name="xyz" id="xyz" class="defaultTextBox"
  cdata="{validate:{required:true, decimal:true, messages:
          {required:'Please enter an decimal value', 
           decimal:'Please enter a valid decimal'}}}">

这工作得很好。现在我还有一个要求是根据页面上的逻辑显示和隐藏某些字段,并且我需要禁用对隐藏字段的验证,以便它们不会干扰表单提交。只需将 required:true 切换为 false 再返回 true 就足够了。只是我不知道怎么办。

有人有这方面的经验吗?

I am using MVC to create forms that are generated at runtime. For validation, I am trying my hand at the jQuery validation library which is very convenient to use. I have the validation expression of each field in the cdata attribute of the tag

<input type="text" name="xyz" id="xyz" class="defaultTextBox"
  cdata="{validate:{required:true, decimal:true, messages:
          {required:'Please enter an decimal value', 
           decimal:'Please enter a valid decimal'}}}">

This works beautifully. Now one more requirement I have is that some fields are being shown and hidden according to the logic on the page and I need to disable the validation on the hidden fields such that they do not interfere with the form submission. Just toggling the required:true to false and back to true should be enough. Only i do not know how.

Anyone has any experience with that?

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

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

发布评论

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

评论(5

物价感观 2024-09-09 02:17:14

只需添加忽略规则并定义选择器即可。
在此示例中,验证将忽略所有具有 class="ignore" 的元素

$("#myform").validate({
   ignore: ".ignore"
})

Just add the ignore rule and define the selector.
In this example, the validation will ignore all elements that have the class="ignore"

$("#myform").validate({
   ignore: ".ignore"
})
知你几分 2024-09-09 02:17:14

如果您使用 ASP.NET MVC Unobtrusive JQuery 验证,则需要以这种方式进行设置。这是因为 Microsoft 实际上调用 jQuery 验证的方式。在 ready 方法中执行此操作应该是安全的。

编辑:在复制粘贴此内容之前,请参阅下面 Cory 的评论。这是我的原始代码

    $("form").data("validator").settings.ignore = ".data-val-ignore, :hidden, :disabled";

然后我只需将 .data-val-ignore 类应用于不进行验证的事物。

请注意,您可能需要添加 :hidden,这实际上是 jquery.validate.js 中定义的默认忽略行为。我也喜欢添加 :disabled

$.extend($.validator, {
defaults: {
    messages: {},
    groups: {},
    rules: {},
    errorClass: "error",
    validClass: "valid",
    errorElement: "label",
    focusInvalid: true,
    errorContainer: $([]),
    errorLabelContainer: $([]),
    onsubmit: true,
    ignore: ":hidden",              // default for ignore in jquery.validate.js
    ignoreTitle: false,
    onfocusin: function( element, event ) {
        this.lastActive = element;

最后,您可能想要设置它的样式 - 在调试期间特别有用。

.data-val-ignore
{
    background: #eee;
}

If you're using ASP.NET MVC Unobtrusive JQuery validation you need to set the settings in this way. This is because of the way Microsoft actually calls jQuery validate. This should be safe to do inside a ready method.

Edit: Please see Cory's comment below before copy pasting this. This is my original code

    $("form").data("validator").settings.ignore = ".data-val-ignore, :hidden, :disabled";

Then I just apply .data-val-ignore class to things not to validate.

Note that you'll probably want to add :hidden which is actually the default ignore behavior defined in jquery.validate.js. I like to add :disabled too.

$.extend($.validator, {
defaults: {
    messages: {},
    groups: {},
    rules: {},
    errorClass: "error",
    validClass: "valid",
    errorElement: "label",
    focusInvalid: true,
    errorContainer: $([]),
    errorLabelContainer: $([]),
    onsubmit: true,
    ignore: ":hidden",              // default for ignore in jquery.validate.js
    ignoreTitle: false,
    onfocusin: function( element, event ) {
        this.lastActive = element;

And finally you may want to style it - especially useful during debugging.

.data-val-ignore
{
    background: #eee;
}
ˉ厌 2024-09-09 02:17:14

按照加布的回答,您还可以将忽略值设置为默认值,这样您就不必在每个表单上设置它。

$.validator.setDefaults({ignore: ".ignore"});

另请注意,忽略字段是 jQuery not() 函数中使用的 jQuery 选择器,因此可以使用任何有效的选择器,而不仅仅是简单的类。

另请参阅http://docs.jquery.com/Plugins/Validation/validate#toptions 有关可设置的其他默认值的详细信息。

Following on Gabe's answer, you can also set the ignore value as a default, so you don't have to set it on each form.

$.validator.setDefaults({ignore: ".ignore"});

Also note, the ignore field is a jQuery selector that is used in the jQuery not() function so any valid selector can be used, not just simple classes.

See also http://docs.jquery.com/Plugins/Validation/validate#toptions for details on other default values that can be set.

还不是爱你 2024-09-09 02:17:14

我都使用 $('.multiselect').rules('remove'); 度过了更好的时光

在我的例子中,无论出于何种原因添加 .cancel.data, -val-忽略
$.validator.setDefaults$('.multiselect') 都没有修复它。

我也尝试了

$('select[multiple][data-val]').removeAttr('data-val').removeAttr('data-val-number').addClass('data-val-ignore').validate({ ignore: "[multiple]" });
$.validator.setDefaults({ ignore: ":hidden,:disabled,.data-val-ignore" });
$('.multiselect').closest('form').data('validator').settings.ignore = ":hidden,:disabled,.data-val-ignore, .data-val-ignore *";
$('.multiselect').data('validator').settings.ignore = "[multiselect]"

其中每一个......以及它们的组合

我的jquery.validate.js

/*! jQuery Validation Plugin - v1.11.0 - 2/4/2013
* https://github.com/jzaefferer/jquery-validation
* Copyright (c) 2013 Jörn Zaefferer; Licensed MIT */

jquery是

jQuery JavaScript Library v1.9.1 - Date:2013-2-4

I had a better time with $('.multiselect').rules('remove');

in my case for whatever reason adding .cancel or .data-val-ignore
to both the $.validator.setDefaults and $('.multiselect') did not fix it.

I also tried

$('select[multiple][data-val]').removeAttr('data-val').removeAttr('data-val-number').addClass('data-val-ignore').validate({ ignore: "[multiple]" });
$.validator.setDefaults({ ignore: ":hidden,:disabled,.data-val-ignore" });
$('.multiselect').closest('form').data('validator').settings.ignore = ":hidden,:disabled,.data-val-ignore, .data-val-ignore *";
$('.multiselect').data('validator').settings.ignore = "[multiselect]"

each of those... and combinations of them

my jquery.validate.js was

/*! jQuery Validation Plugin - v1.11.0 - 2/4/2013
* https://github.com/jzaefferer/jquery-validation
* Copyright (c) 2013 Jörn Zaefferer; Licensed MIT */

jquery was

jQuery JavaScript Library v1.9.1 - Date: 2013-2-4

笙痞 2024-09-09 02:17:14

您可以通过执行以下操作从单个字段中删除规则:

 $("#field").rules('remove', 'required');

其中第二个参数是规则名称。我还删除了与此规则关联的属性以避免任何混淆:

 $("#field").removeAttr('required');     

You can remove a rule from a single field by doing the following:

 $("#field").rules('remove', 'required');

Where the second parameter is the rule name. I also remove the attribute associated with this rule to avoid any confusion:

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