MVC 3, View Model for user registration process. Password validation not working properly
- by sec_goat
I am trying to create a user registration page using MVC 3, so that I can better understand the process of how it works, what's going on behind the scenes etc. I am running into some issues when trying to use [Compare] to check to see that the user entered the same password twice.
I tried adding the ComparePassword field to my user model first, and found that would not work the way I wanted as I did not have the field in the database, so the obvious answer was to create a View Model using the same information including the ComparePassword field.
So I now have created a User model and a RegistrationViewModel, however it appears that the [Compare] on the password is not returning anything, for instance no matter what I put in the two boxes, when I click create it gives no error, which seems to me to mean it was successfully validated.
I am not sure what I am doing or not doing to make this work properly. I have tried updating the jQuery.Validate to the newest version as there were some bugs reported in older version, this has not helped my efforts.
Below is a wall of code, that is what I am working with.
}
public class RegistrationViewModel
{
[Required]
[StringLength(15, MinimumLength = 3)]
[Display(Name = "User Name")]
[RegularExpression(@"(\S)+", ErrorMessage = " White Space is not allowed in User Names")]
[ScaffoldColumn(false)]
public String Username { get; set; }
[Required]
[StringLength(15, MinimumLength = 3)]
[Display(Name = "First Name")]
public String firstName { get; set; }
[Required]
[StringLength(15, MinimumLength = 3)]
[Display(Name = "Last Name")]
public String lastName { get; set; }
[Required]
[Display(Name = "Email")]
public String email { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public String password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Re-enter Password")]
[Compare("Password", ErrorMessage = "Passwords do not match.")]
public String comparePassword { get; set; }
}