views:

169

answers:

1

For example, I'd like to validate a user registration form and check whether user has entered his password in "password" and "confirm password" fields, AND that those two values are the same.

Found this but is reflection really the only way?

+1  A: 

You can try this way:

[System.ComponentModel.DataAnnotations.CustomValidation(typeof(Test), "Verify", ErrorMessage = "No match!")]
public class Test
{
    [Required]
    public string Password { get; set; }

    [Required]
    public string ConfirmPassword { get; set; }

    public static ValidationResult Verify(Test t)
    {
        if (t.Password == t.ConfirmPassword)
            return ValidationResult.Success;
        else
            return new ValidationResult("");
    }
}
Cristi Todoran