0

I am looking at asp.net mvc 3.0 and how it uses unobtrusive javascript through the jquery.validate library.

I am having trouble on finding what validation methods are supported. I only can find that mvc 3.0 can used the jquery.validate remote.

Does this new way use still data annotations and is limited by the couple they have or does this use something completely different?

Thanks

chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

1

Client-Side validation is enabled by default in ASP.NET MVC 3. It uses unobtrusive javascript implementation based on the jquery.validate plugin which is also included by default. Unobtrusiveness is achieved thanks to the new HTML5 data-* attributes which are automatically emitted by the standard HTML Helpers based on the model metadata. Client-side validation works out of the box for all standard attributes in the System.ComponentModel.DataAnnotations namespace including the [Remote] attribute (see example here). It will not work out-of-the-box with custom defined attributes deriving from ValidationAttribute or with IValidatableObject interface.

The following section in web.config controls client side validation:

<appSettings>
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • So if I want to use Max,Min,credit card,compare and etc from jquery validate I have to write my own dataAnnotation? – chobo2 Jan 18 '11 at 17:18
  • @chobo2, for Min and Max you could use the [`Range`](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx) attribute. For credit card you need custom code or try the `RegularExpression` attribute but I am not sure whether you could validate a credit card number with regex. You might also checkout the [following blog post](http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html) which goes into more details. – Darin Dimitrov Jan 18 '11 at 17:39
  • I don't get the "Bridging HTML and jQuery Validate: Adapters" part. It seems like if I want to make my own ones to use jquery validate I have to write some adapters but it does not go into how to make them. It also looks like there are some already made but it does not seem to show you how to use them. – chobo2 Jan 19 '11 at 02:57
  • 1
    This might help: http://samipoimala.com/it/2010/11/29/unobtrusive-client-validation-in-asp-net-mvc-3/comment-page-1/#comment-3685 – Paul Hiles Jan 20 '11 at 15:13
  • @chobo2, you may checkout this answer: http://stackoverflow.com/questions/4747184/perform-client-side-validation-for-custom-attribute/4747466#4747466 – Darin Dimitrov Jan 20 '11 at 18:22
  • @The Flower Guy - So the validation adapters that are already supported how would I hook them up with ones? Like if you look at Darin link it seems like there are alot of adapters setup. How do I use them? – chobo2 Jan 25 '11 at 21:20