问题
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