DataAnnotation to compare two properties

As one possibe option self-validation:

Implement an interface IValidatableObject with method Validate, where you can put your validation code.

public class TestModel : IValidatableObject
{
    public string Email{ get; set; }
    public string ConfirmEmail { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Email != ConfirmEmail)
        {
            yield return new ValidationResult("Emails mismatch", new [] { "ConfirmEmail" });
        }
    }
}

Please notice: this is only server-side validation.


Use the CompareAttribute

public string EmailAddress {get; set;}

[Compare(nameof(EmailAddress), ErrorMessage = "Emails mismatch")]
public string VerifiedEmailAddress { get; set; }