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

.Net Mvc 3 Trigger (other than submit button) Unobtrusive Validation

$(‘form’).valid() should work. Let’s exemplify. Model: public class MyViewModel { [Required] public string Foo { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } } View: @model MyViewModel <script src=”https://stackoverflow.com/questions/6301492/@Url.Content(“~/Scripts/jquery.validate.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/6301492/@Url.Content(“~/Scripts/jquery.validate.unobtrusive.js”)” type=”text/javascript”></script> @using (Html.BeginForm()) { @Html.LabelFor(x => x.Foo) @Html.EditorFor(x => x.Foo) @Html.ValidationMessageFor(x => … Read more

MVC : How to enable client side validation on hidden fields

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, … Read more

Unobtrusive validation doesn’t work with Ajax.BeginForm

That’s because the second view is loaded with AJAX at a later stage and you need to call $.validator.unobtrusive.parse(…) immediately after its contents is injected into the DOM in order to enable unobtrusive validation. Look at the following blog post for more details. So in your case, instead of alerting in the OnSuccess callback of … Read more

Best Practices ViewModel Validation in ASP.NET MVC

To answer your 3th question first: No there is no easier way then what you are doing. Two lines of code to get it working can hardly be easier. Although there is a plug-in you could use, like explained in the question unobtrusive validation not working with dynamic content Your first question, how to centralize … Read more

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

Validation type names in unobtrusive client validation rules must be unique

JimmiTh’s comment on the question provided a key insight for me to resolve this for myself. In my case, I definitely did add an additional provider to ModelValidatorProviders. I added a custom validation factory (using Fluent Validation) with this code in my Global.asax.cs file: ModelValidatorProviders.Providers.Add( new FluentValidationModelValidatorProvider(validatorFactory)); But using multiple providers isn’t necessarily problematic. What … Read more

MVC3 Unobtrusive Validation Not Working after Ajax Call

$.validator.unobtrusive.parse(“#frmAddItem”); will work. Do note that it must be in the partial that you load through ajax (below the form in the partial) <form id=”frmAddItem” method=”POST” action=”…”> <!– all the items –> </form> <script type=”text/javascript”> $.validator.unobtrusive.parse(“#frmAddItem”); </script>

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.