Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC data annotation to compare one property to another?

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)?

like image 764
devlife Avatar asked Mar 15 '10 22:03

devlife


3 Answers

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; }
like image 107
Cillié Malan Avatar answered Oct 10 '22 23:10

Cillié Malan


Here you go: http://www.dotnetguy.co.uk/post/2010/01/09/Property-Matching-With-Data-Annotations.aspx
Edit: New link: http://www.dotnetguy.co.uk/post/2010/01/09/property-matching-with-data-annotations/

like image 21
Dustin Laine Avatar answered Oct 11 '22 00:10

Dustin Laine


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; }
like image 33
Mitch Avatar answered Oct 11 '22 00:10

Mitch