Question

For one of my project requirement, i need to perform validation over some control. (MVC 5 , Data annotation, JQuery)

Looking some different approach, i need data annotation validation handled through JQuery

  I have following control on my page with validation using data annotation on all these control:

  1) Username
  2) EmailId
  3) Contact No
  4) Password
  5) Confirm Password

Initially top 1-3 controls are blocked and and 4-5 control is set to display none So, i need validation should not check for 4-5 when i press submit button.

Later it will show 4-5 control and 1-3 control is set to display none. in that case i need validation should not check for 1-3.

NOTE: There is only one View page, can't have multiple view pages

How can i enable and disable data annotation validation using JQuery.

Était-ce utile?

La solution

i think you are looking for conditional validation. various post for this:-

Model Validation / ASP.NET MVC 3 - Conditional Required Attribute

http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx

http://www.codeproject.com/Articles/541244/Conditional-ASP-NET-MVC-Validation-using-Data-Anno

Client side validation

If you are using jqueryvalidation.js by default it skips validation for hidden and display none element and if you want ignore validation for specific inputs you can do it by:-

$("#form1").validate({
  ignore: "input[type='text']:hidden",
   rules: {
     something: {
         number:true,
         min:1,
         required:true
        }
    }
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top