2

I am working on MVC razor. I have to do validation depend on condition. Condition is below mentioned

  1. We have country dropdown which is having Country USA and UK. when i select USA from Drop down then I do not want any validation on city and zip text box.

    2.When I select US from drop down then i want validation on city and zip text box.

How to implement this scenario on mvc razor.

user1624306
  • 311
  • 1
  • 4
  • 10

2 Answers2

0

You can do this using jQuery validation.

if(...)
    $('#target').rules('add', { ... });
else
    $('#target').rules('remove');

Or you can add custom method:

jQuery.validator.addMethod(...);

See: http://docs.jquery.com/Plugins/Validation/Validator/addMethod

karaxuna
  • 26,752
  • 13
  • 82
  • 117
0

For the client side you have to do the validation yourself by creating custom validation method. You can see an example here.

At the server-side I may suggest you to go for implementing IModelValidatable in the model class. You can get more information about this in this blog post. I also suggest you may create an Address model that wraps up the properties and validation so you don't need to repeat the validation in many models.

Community
  • 1
  • 1
VJAI
  • 32,167
  • 23
  • 102
  • 164