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?
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.
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>
I think you need to write a custom validation attribute and then handle the data yourself in JQuery.
See this article: http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
来源:https://stackoverflow.com/questions/12176205/disable-validation-for-an-element-with-jquery-unobtrusive-validation