MVC 3 Validation for confirm email address field

10,216

Solution 1

Yep - for example:

[Compare("Email", ErrorMessage = "The email and confirmation do not match.")]

Hope that helps.

Solution 2

Take a look at the CompareAttribute

[Compare("Email", ErrorMessage = "The email and confirmation email do not match.")]
public string ConfirmEmail { get; set; }
Share:
10,216
David MZ
Author by

David MZ

Updated on June 14, 2022

Comments

  • David MZ
    David MZ about 2 years

    Is there a way using MVC data validation attributes to validate client side if two fields on my model are equal.

    I have two fields:

        [Required(ErrorMessage = "*")]
        [Email(ErrorMessage = "*")]
        public string Email { get; set; }
    
        [Required(ErrorMessage = "*")]
        [Email(ErrorMessage = "*")]
        public string ConfirmEmail { get; set; }
    

    I want to be able to add an attribute that those two fields should be equel and if not an validatio error will appear. Is there a way to do so?

    Thank you.

  • Timbo
    Timbo almost 13 years
    @Adam Tuliper - actually what's odd is that I answered slightly before Russ (2 seconds originally), then he added my answer to his (note the same 'ErrorMessage'), yet in the end mine got accepted. So indeed, it is funny how it works!