@Scripts
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery.validate.js")"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"></script>
@View
@using (Html.BeginForm("TestModelState", "RandD", FormMethod.Post, new {id="form123" })) { @Html.TextBoxFor(x => x.htmlText, new { style="display:none"})<br /> @Html.ValidationMessageFor(x => x.htmlText) <div> @Html.TextBoxFor(x => x.Uprop1)<br /> @Html.ValidationMessageFor(x => x.Uprop1) </div> <input type="submit" value-="Submit" onclick="abc()" /> }
WHAT I have tried
var validateMeta = $('#form123').validate(); validateMeta.settings.ignore = "";
$.validator.setDefaults({ignore: ""});
$.validator.setDefaults({ ignore: [] });
We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level. For client-side validation, the values of above both the keys must be true.
It turns out that to enable client side validation without using the HtmlHelpers and a model you just have to add an input with data-val="true" and then data-val- followed by validation method that you want to apply (e.g. data-val-required ), the value of which will be the error message presented to the user (e.g. data ...
You can disable the unobtrusive validation from within the razor code via this Html Helper property: HtmlHelper. ClientValidationEnabled = false; That way you can have unobtrusive validation on and off for different forms according to this setting in their particular view/partial view.
I ran into this same problem. A hidden field on a page was storing a user ID, which was selected from an autocomplete text box. This ID had validation to ensure that a non-zero id was posted back. We wanted to include this validation in the client side.
By default jQuery validate ignores hidden fields, elements with zero width and height, those with css display:none and any elements with an invisible parent (using same criteria).
The ignore setting can however be overridden by adding the following script (I just added it to my custom validator script before calling $.validator.addMethod()):
// By default validator ignores hidden fields. // change the setting here to ignore nothing $.validator.setDefaults({ ignore: null });
NOTE: This code won't work if it's run inside the document ready
or jQuery $(function () {})
method.
Just after you insert the script for validation set the ignore to "":
@section Scripts { @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> $.validator.setDefaults({ ignore: "" }); </script> }
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