Disable validation for an element with jQuery Unobtrusive Validation

天涯浪子 提交于 2019-11-27 13:22:30

问题


By some form generator a list of elements is rendered on a page and they all have validations on them. When I look in the HTML source, i see something like this:

<input type="text" id="email" name="email" data-val-required="No valid email address!" data-val="true">

I need to somehow have a dynamic way to enable/disable the validation for such an element. I tried to enable/disable the data-val attribute, by setting it to false and then back to true. But it doesn't seem to be responding to that. The validation is always there!

Anyone has any idea how I can enable/disable validations on certain fields in a dynamic way?


回答1:


I actually found a solution that fits my needs better. I can do the following:

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    settngs.ignore = ".ignore";
});

And with that i can 'toggle' any element that i want by adding or removing the classname ignore from an element.




回答2:


I think this will help.

<div class="editor-field">
       @{ Html.EnableClientValidation(false); }
       @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })
       @{ Html.EnableClientValidation(true); }
</div>



回答3:


This worked better for me:

$('#Reference').rules('add', 'required');
$('#Reference').rules('add', 'remove');
$('#Reference').rules('add', { minlength: 2 });


来源:https://stackoverflow.com/questions/12176205/disable-validation-for-an-element-with-jquery-unobtrusive-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!