ASP.NET CompareValidatorControl
Validation Server Controls
Definition and Usage
The CompareValidator control is used to compare the value of an input control with the value of another input control or a constant value.
Note:If the input control is empty, no validation functions are called and validation will succeed. Use the RequiredFieldValidator control to make the field required.
Attributes
| Attribute | Description |
|---|---|
| BackColor | The background color of the CompareValidator control. |
| ControlToCompare | The name of the control to compare with the control being validated. |
| ControlToValidate | The id of the control to validate. |
| Display | The display behavior of the validation control. Legal values are:
|
| EnableClientScript | Boolean value that specifies whether client-side validation is enabled. |
| Enabled | Boolean value that specifies whether the validation control is enabled. |
| ErrorMessage | The text to display in the ValidationSummary control when validation fails.Note:If the Text property is not set, the text will also be displayed in the validation control. |
| ForeColor | The foreground color of the control. |
| id | The unique id of the control. |
| IsValid | Boolean value that indicates whether the control specified by ControlToValidate passes validation. |
| Operator | The type of comparison operation to perform. Operators are:
|
| runat | Specifies that the control is a server control. Must be set to "server". |
| Text | The message displayed when validation fails. |
| Type | Specifies the data type of the value to compare. Types are:
|
| ValueToCompare | A constant value to compare with the value entered by the user into the input control being validated. |
Examples
CompareValidator
In this example, we declare two TextBox controls, a Button control, and a CompareValidator control in a .aspx file. If validation fails, the text "Validation Failed!" will be displayed in the CompareValidator control with a yellow background and red font.
CompareValidator 2
In this example, we declare two TextBox controls, a ListBox control, a Button control, and a CompareValidator control in a .aspx file. The check_operator() function sets the operator selected in the ListBox control as the operator of the CompareValidator control, and then validates the CompareValidator control. If validation fails, the text "Validation Failed!" will be displayed in the CompareValidator control with a yellow background and red font.
Validation Server Controls Other Extensions