jqueryasp.net-mvc-5asp.net-mvc-validation

Change ASP.NET MVC validation dynamically in Jquery


On one of the view model fields, I have set the min length in ASP.NET MVC Data Annotation attribute.

[MinLength(5, ErrorMessage = "A minimum of 5 digits is required")]

Based on a dropdown selection(which has 2 values), MinLength needs to be updated to 10. ClientSideValidation is enable and so validation needs to reflect the changes before it is posted back to the controller.

Tried changing the attribute "data-val-minlength-min" using jQuery based on dropdown value, but it did not change the validation.

Any suggestions please.


Solution

  • @StephenMuecke gave me the solution I needed. It works on client side and server side as desired. I am posting it as answer so that the next person can easily see it.

    Create you own conditional validation attribute so that you get both client and server side validation - The Complete Guide To Validation In ASP.NET MVC 3 - Part 2