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?
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With