Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value.
See Also: CompareValidator Members
Use the System.Web.UI.WebControls.CompareValidator control to compare the value entered by the user in an input control, such as a System.Web.UI.WebControls.TextBox control, with the value entered in another input control or a constant value. The System.Web.UI.WebControls.CompareValidator control passes validation if the value of the input control matches the criteria specified by the CompareValidator.Operator, CompareValidator.ValueToCompare, and/or CompareValidator.ControlToCompare properties.
You can also use the System.Web.UI.WebControls.CompareValidator control to indicate whether the value entered in an input control can be converted to the data type specified by the BaseCompareValidator.Type property.
Specify the input control to validate by setting the BaseValidator.ControlToValidate property. If you want to compare a specific input control with another input control, set the CompareValidator.ControlToCompare property to specify the control to compare with.
If the value entered in the input control specified by the CompareValidator.ControlToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, but the value entered in the input control specified by the BaseValidator.ControlToValidate property can be converted, the input control being validated is considered valid. Therefore, you should also place a System.Web.UI.WebControls.CompareValidator or System.Web.UI.WebControls.RangeValidator control on the input control specified by the CompareValidator.ControlToCompare property.
Instead of comparing the value of an input control with another input control, you can compare the value of an input control to a constant value. Specify the constant value to compare with by setting the CompareValidator.ValueToCompare property.
If the value specified by the CompareValidator.ValueToCompare property cannot be converted to the data type specified by the BaseCompareValidator.Type property, an exception is thrown. Be sure to check the data type of a value before programmatically assigning it to the CompareValidator.ValueToCompare property.
Do not set both the CompareValidator.ControlToCompare and the CompareValidator.ValueToCompare property at the same time. You can either compare the value of an input control to another input control, or to a constant value. If both properties are set, the CompareValidator.ControlToCompare property takes precedence.
Use the CompareValidator.Operator property to specify the type of comparison to perform, such as greater than, equal to, and so on. If you set the CompareValidator.Operator property to ValidationCompareOperator.DataTypeCheck, the System.Web.UI.WebControls.CompareValidator control ignores the CompareValidator.ControlToCompare and CompareValidator.ValueToCompare properties and simply indicates whether the value entered in the input control can be converted to the data type specified by the BaseCompareValidator.Type property.
The BaseCompareValidator.Type property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed.
When the BaseCompareValidator.Type property is set to ValidationDataType.Date and the current calendar type is non-Gregorian, the validator performs server-side validation only. The validator client script supports only Gregorian calendars.
The following table lists the various data types that can be compared.
String |
A string data type. |
Integer |
A 32-bit signed integer data type. |
Double |
A double-precision floating-point number data type. |
Date |
A date data type. |
Currency |
A decimal data type that can contain currency symbols. |
If the input control is empty, no validation functions are called and validation succeeds. Use a System.Web.UI.WebControls.RequiredFieldValidator control to require the user to enter data in the input control.
When you use the System.Web.UI.WebControls.CompareValidator control inside an System.Web.UI.UpdatePanel control, make sure that the validator control and the control it is associated with are in the same panel. For more information about using the System.Web.UI.UpdatePanel control for partial-page updates, see Partial-Page Rendering Overview.
For more information about validation controls, see System.Web.UI.WebControls.BaseValidator.
For information about how to configure this control so that it generates markup that conforms to accessibility standards, see Accessibility in Visual Studio 2010 and ASP.NET 4 and ASP.NET Controls and Accessibility.