Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable validation for an element with jQuery Unobtrusive Validation

I am generating a list of elements on a page, and they all have validators attached to them. When I look in the HTML source, I see something like:

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

I need a way to dynamically 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 response to that; the validation is always there!

Does anyone have any idea how I can enable/disable validators on certain fields dynamically?

like image 732
w00 Avatar asked Aug 29 '12 10:08

w00


1 Answers

I would add one more option without overriding unobtrusive validator defaults. As validation for specific element is controled by data-val attribute, you can set it as data-val="false".

So for Razor helper, use:

@Html.TextBoxFor(model => Model.DateOfBirth, new { @class = "form-control", data_val = false })

and for new .net core syntax, use:

<input asp-for="DateOfBirth" class="form-control" data-val="false">
like image 175
David Bouška Avatar answered Oct 14 '22 04:10

David Bouška