Disable validation for an element with jQuery Unob

2019-01-22 15:08发布

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?

3条回答
唯我独甜
2楼-- · 2019-01-22 15:34

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>
查看更多
The star\"
3楼-- · 2019-01-22 15:40

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.

查看更多
4楼-- · 2019-01-22 15:55

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

查看更多
登录 后发表回答