We will be exploring Compare
validation in this section, and since the steps are common for all the built-in validations except the pattern, we will be covering only the validation pattern in the other validation sections. The compare validation will avoid the round-trip between the UI and the service, providing client-side validation for comparing passwords, emails, or even sensitive information such as bank account numbers, which require this compare validation to be in place.
The validation error can be captured in the Compare
data annotation in the following scenarios:
If the field is
NULL
or empty or a whitespaceIf the field has a value which doesn't match the configured field value
The Compare
attribute/data annotation can be configured as follows:
public class RegistrationViewModel
{
// Code removed for brevity
[Required(ErrorMessage = "ConfirmPassword is required")]
[Compare("Password")]
public string ConfirmPassword { get; set; }
...