ASP.NET MVC 3: Required steps for unobtrusive client-side validation of dynamic/AJAX content

At this point I believe the following is a complete set of requirements: Create a form with Html.BeginForm Turn on ClientValidationEnabled Turn on UnobtrusiveJavaScriptEnabled Set appropriate validation attributes on the model’s properties (not fields) If the Html Helpers being used to create the form elements are not on the same form as the Html.BeginForm call, … Read more

Unobtrusive validation not working on dynamically-added partial view

Ok, I am going to start over with a new answer here. Before you call $.validator.unobtrusive.parse, remove the original validator and unobtrusive validation from the form like so: var form = $(“#main_div”).closest(“form”); form.removeData(‘validator’); form.removeData(‘unobtrusiveValidation’); $.validator.unobtrusive.parse(form); This same answer is documented here.

Unobtrusive validation in Chrome won’t validate with dd/mm/yyyy

Four hours later I finally stumbled across the answer. For some reason Chrome seems to have some inbuilt predilection to use US date formats where IE and FireFox are able to be sensible and use the regional settings on the OS. jQuery.validator.methods[“date”] = function (value, element) { return true; }

Error in jquery.validate.js in MVC 4 Project with jQuery 1.9

This issue is fixed in jQuery Validation Plugin 1.11.0pre. Unfortunately there is currently no pre-release build on NuGet, so it is currently necessary to download jquery.validation.js directly from GitHub: jQuery.Validation 1.11 is now available via NuGet (thanks @Simon_Weaver). UPDATE It looks like general support for jQuery 1.9 is in the jQuery.Validation code base but not … Read more

Revalidating a modified ViewModel within a controller method?

Once you have removed the offending item(s), clear the ModelState and validate again, like so: ModelState.Clear(); TryValidateModel(crew); // assumes the model being passed is named “crew” Note: Be carefull when use TryValidateModel method because this method does not validate nested object of model (As mentioned by @Merenzo).

MVC 4 client side validation not working

I had the same problem. It seems that the unobtrusive validation scripts were not loaded (see screenshot at the end). I fixed it by adding at the end of _Layout.cshtml @Scripts.Render(“~/bundles/jqueryval”) The end result: @Scripts.Render(“~/bundles/jquery”) @Scripts.Render(“~/bundles/jqueryval”) @RenderSection(“scripts”, required: false) Except for my pretty standard CRUD views everything is Visual studio project template defaults. Loaded scripts … Read more

How to change ‘data-val-number’ message validation in MVC while it is generated by @Html helper

You can override the message by supplying the data-val-number attribute yourself when rendering the field. This overrides the default message. This works at least with MVC 4. @Html.EditorFor(model => model.MyNumberField, new { data_val_number=”Supply an integer, dude!” }) Remember that you have to use underscore in the attribute name for Razor to accept your attribute.

jquery.validate.unobtrusive not working with dynamic injected elements

I tried Xhalent’s approach but unfortunately it wasn’t working for me. Robin’s approach did work and didn’t work. It worked great for dynamically added elements, but if you tried to use JQuery to remove all the validation attributes and spans from the DOM, the validation library still would try to validate them. However, if you … Read more