My User model has these data annotations to validate input fields:
[Required(ErrorMessage = "Username is required")]
[StringLength(16, ErrorMessage = "Must be between 3 and 16 characters", MinimumLength = 3)]
public string Username { get; set; }
[Required(ErrorMessage = "Email is required"]
[StringLength(16, ErrorMessage = "Must be between 5 and 50 characters", MinimumLength = 5)]
[RegularExpression("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", ErrorMessage = "Must be a valid email")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required"]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
public string Password { get; set; }
[Required(ErrorMessage = "Confirm Password is required"]
[StringLength(255, ErrorMessage = "Must be between 5 and 255 characters", MinimumLength = 5)]
public string ConfirmPassword { get; set; }
However, I’m having trouble working out how to make sure Confirm Password is the same as Password. As far as I know only these validation routines exist: Required, StringLength, Range, RegularExpression.
What can I do here? Thanks.
If you are using
ASP.Net MVC 3, you can useSystem.Web.Mvc.CompareAttribute.If you are using
ASP.Net 4.5, it’s in theSystem.Component.DataAnnotations.EDIT: For
MVC2Use the below Logic, UsePropertiesMustMatchinsteadCompareattribute [Below code is copied from the defaultMVCApplicationproject template.]