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

.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

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

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

Manually set unobtrusive validation error on a textbox

First, you can add a validation summary: @Html.ValidationMessageFor(m => m.Slug) Then you can manually trigger jQuery validation / unobtrusive validation with the .showError method. Here is a sample: var errorArray = {}; errorArray[“Slug”] = ‘Some error message for the Slug text box’; $(‘#SomeFormId’).validate().showErrors(errorArray);

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

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