No password matching on MVC side

I have the following (abbreviated) DTO for registering a new user:

[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")] public class RegisterModel { //..... [DataType(DataType.Password)] public string Password { get; set; } [DataType(DataType.Password)] public string ConfirmPassword { get; set; } } 

Then it is wrapped in a view model as such:

 public class RegisterModelViewData: BaseViewData { public RegisterModel RegisterModel { get; set; } public int PasswordLength { get; set; } } 

And finally, in the view, I have two fields as such:

 <div class="editor-field"> <%= Html.PasswordFor(m => m.RegisterModel.Password) %> <%= Html.ValidationMessageFor(m => m.RegisterModel.Password) %> </div> <div class="editor-field"> <%= Html.PasswordFor(m => m.RegisterModel.ConfirmPassword) %> <%= Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword) %> </div> 

Apparently, I should get client-side validation and not write if the passwords do not match. I get a message, and then a message saying "Creating a wA account failed", but nothing about the mismatched passwords. I briefly omitted the Required and MininumLength attributes from passwords for brevity, but they seem to behave as expected and check on the client.

+4
source share
2 answers

Properties MustMatch is a type validator, not a property validator, and MVC does not support client-side validation for type validators, only to validate properties. In a fascinating ASP.NET MVC blog post : adding client-side validation to the PropertiesMustMatchAttribute property , Stuart Licks describes how to implement MustMatch , which uses client-side validation and can be used to match two properties, for example, to confirm a password.

+1
source

Now available in ASP.MVC 3 if anyone is still interested.

 public string Password { get; set; } [Compare("Password", ErrorMessage = "Passwords must match")] public string ConfirmPassword { get; set; } 
+17
source

All Articles