17

I've been playing around data annotations in MVC2 and am curious if there is an annotation to compare 2 properties (ie. password, confirm password)?

devlife
  • 15,275
  • 27
  • 77
  • 131

4 Answers4

42

If you are using ASP.Net MVC 3, you can use System.Web.Mvc.CompareAttribute

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirm { get; set; }
Greg B
  • 14,597
  • 18
  • 87
  • 141
Cillié Malan
  • 864
  • 11
  • 13
2

System.Web.Mvc.CompareAttribute has been deprecated.

I was able to modify to work like this:

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
Mitch
  • 85
  • 2
  • 11
0

There's not one built in, however, you can make your own. See this link, which shows the "PropertiesMustMatchAttribute" that does just what you're looking for.

David Morton
  • 16,338
  • 3
  • 63
  • 73