David MZ
David MZ

Reputation: 3718

MVC 3 Validation for confirm email address field

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.

Upvotes: 16

Views: 10514

Answers (2)

Russ Cam
Russ Cam

Reputation: 125538

Take a look at the CompareAttribute

[Compare("Email", ErrorMessage = "The email and confirmation email do not match.")]
public string ConfirmEmail { get; set; }

Upvotes: 16

Timbo
Timbo

Reputation: 4533

Yep - for example:

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

Hope that helps.

Upvotes: 36

Related Questions