MVC3 Validation – Require One From Group

Here’s one way to proceed (there are other ways, I am just illustrating one that would match your view model as is): [AttributeUsage(AttributeTargets.Property)] public class RequireAtLeastOneOfGroupAttribute: ValidationAttribute, IClientValidatable { public RequireAtLeastOneOfGroupAttribute(string groupName) { ErrorMessage = string.Format(“You must select at least one value from group \”{0}\””, groupName); GroupName = groupName; } public string GroupName { get; … 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.

client-side validation in custom validation attribute – asp.net mvc 4

Take a look at this: http://thewayofcode.wordpress.com/tag/custom-unobtrusive-validation/ Using this tutorial I got my custom validation code running with no problem. The only difference I can spot in your code is the way you created the $.validator.unobtrusive.adapters.add function. The parameters are a little bit different but, maybe, the problem is just that you have not defined the … Read more

unobtrusive validation not working with dynamic content

If you try to parse a form that is already parsed it won’t update What you could do when you add dynamic element to the form is either You could remove the form’s validation and re validate it like this: var form = $(formSelector) .removeData(“validator”) /* added by the raw jquery.validate plugin */ .removeData(“unobtrusiveValidation”); /* … Read more

attaching jquery validation to replacement element

The plugin you’re using hides the input and replaces it with its own html. By default, jQuery validation does not validate hidden form controls but you can override this behavior by modifying the validator $.validator.setDefaults({ ignore: [] }); Note that if you have other hidden elements that you don’t want validated, then you could give … Read more

ASP.NET MVC implement custom validator use IClientValidatable

You have messed up your script inclusions. In your _Layout you have included the following scripts in that order: <script src=”https://stackoverflow.com/questions/8284207/@Url.Content(“~/Scripts/jquery.validate.min.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/8284207/@Url.Content(“~/Scripts/jquery.validate.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/8284207/@Url.Content(“~/Scripts/jQuery.IsDateAfter.js”)” type=”text/javascript”></script> Now obviously jquery.validate.min.js and jquery.validate.js represents the same script, the first being the minified version. But since you haven’t included the jquery.validate.unobtrusive.js script (this is done much … 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