title: asp.net mvc client side validation
body: <p>I've been tinkering with the client side validation features in ASP.net MVC after reading <a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" rel="nofollow">ScottGU's blog post</a> on the subject. It's pretty easy to use the System.Componentmodel.DataAnnotations attributes like this:</p>
  
  <pre><code>    [Required(ErrorMessage = "You must specify a reason")]
      public string ReasonText { get; set; }
  </code></pre>
  
  <p>... but what happens if you need something just a little more complex. What if you have an Address class with a PostalCode and CountryCode field. You would want to validate the postal code against a different regex for each country. [0-9]{5} works for the USA, but you need a different one for Canada. </p>
  
  <p>I got around that by rolling my own ValidationService class that takes the ModelState property of the controller and validates it accordingly. This works great on the server side but doesn't work with the fancy new client side validation. </p>
  
  <p>In Webforms I would use javascript-emitting controls like RequiredFieldValidator or CompareValidator for the easy stuff and then use a CustomValidator for the complex rules. This way I have all my validation logic in one place, and I get the benefit of rapid javascript validation for simple stuff (90% of the time) while I still get the security of server side validation as a backstop. </p>
  
  <p>What would be the equivalent approach in MVC? </p>