Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No client side password matching on MVC view

I have the following (abridged) 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; }
}

This is then wrapped in a View Model as such:

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

And finally, on the view, I have the 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 am supposed to get a client side validation, and no post, if the passwords don't match. I get a post and then a message that "Account creation wAs unsuccessful", but nothing about mismatched passwords. I have omitted the Required and MininumLength attributes from the password proeprties here for brevity, but they seem to behave as expected and validate on the client.

like image 289
ProfK Avatar asked Dec 03 '22 04:12

ProfK


1 Answers

Now available in ASP.MVC 3 in case anyone is still wondering

public string Password { get; set; }
[Compare("Password", ErrorMessage = "Passwords must match")]
public string ConfirmPassword { get; set; }
like image 69
Peter Morris Avatar answered Dec 18 '22 22:12

Peter Morris