Partilhar via


ASP.NET MVC: ValidatePasswordLength and PropertiesMustMatch in ASP.NET MVC 3 RC2

This is the fifth post in what is becoming a mini-series:

In previous posts we looked at how to convert the ValidatePasswordLength and PropertiesMustMatch attributes from the  ASP.NET MVC template to include client-side validation and then looked at the validation improvements in ASP.NET MVC 3 Preview 1. Writing about pre-release technology has some inherent risks, and there have been further changes to the validation infrastructure so this post reviews the situation with ASP.NET MVC 3 RC2 (which should be pretty close to the API for RTM)!

There are a couple of big changes since Preview 1. The first is that client-side validation is enabled by default so (to my mind at least) that means that it is important to support client-side validation for your attributes where possible so that the end user experience is consistent. The second change is that the ASP.NET MVC team have moved validation over to jQuery & jQuery validate, and are now using an Unobtrusive Javascript approach (see Brad Wilson’s post Unobtrusive Client Validation in ASP.NET MVC 3).

So, where does all that leave us in terms of ValidatePasswordLength and PropertiesMustMatch? Well, the good news is that they’re both now client-side enabled out-of-the-box!

ValidatePasswordLength has had client-side support added. It looks up the minimum password length from the Membership provider and then returns a client-side string length validator for that length.

PropertiesMustMatch has morphed into a new Compare attribute that is very similar to the MustMatch attribute from the previous posts.

Overall this is a step forward for the project template and for ASP.NET MVC (although I still wish that the project template didn’t use a single file under the Models folder for all of the account model classes and the validation classes and the service abstractions!)

The previous posts still give some useful information for writing custom validation attributes. At some point I may even post an example of using jQuery validate for the custom client-side validation logic…