Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET CompareValidator Control

ASP.NET CompareValidator Control


Validation Server Controls Validation Server Controls


Definition and Usage

The CompareValidator control is used to compare the value of one input control with the value of another input control or a constant value.

**Note:** If the input control is empty, no validation function is called and validation succeeds. Use the RequiredFieldValidator control to make a field required.


property

property 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 be validated.
Display The display behavior of the validation control. Valid values are:
  • None - The control is not displayed. Used only to display an error message in the ValidationSummary control.
  • Static - If validation fails, the control displays the error message. Even if the input passes validation, space for the message is reserved on the page (i.e., the space for displaying the message is pre-allocated).
  • Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space for the message is reserved on the page (i.e., the space for displaying the message is added dynamically).
  • | | EnableClientScript | Boolean, specifies whether to enable client-side validation. | | Enabled | Boolean, specifies whether to enable the validation control. | | ErrorMessage | The text displayed in the ValidationSummary control when validation fails. **Note:** If the Text property is not set, the text is also displayed in the validation control. | | ForeColor | The foreground color of the control. | | id | The unique id of the control. | | IsValid | Boolean, indicates whether the control specified by ControlToValidate passed validation. | | Operator | The type of comparison operation to perform. The 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 values to compare. The types are: | | ValueToCompare | A constant value to compare with the value entered by the user into the input control being validated. |

    Example

    CompareValidator
    In this example, we declare two TextBox controls, one Button control, and one CompareValidator control in the .aspx file. If validation fails, the text "Validation Failed!" is displayed in the CompareValidator control with a yellow background and red text.

    CompareValidator 2
    In this example, we declare two TextBox controls, one ListBox control, one Button control, and one CompareValidator control in the .aspx file. The check_operator() function sets the operator selected in the ListBox control as the operator of the CompareValidator control, then validates the CompareValidator control. If validation fails, the text "Validation Failed!" is displayed in the CompareValidator control with a yellow background and red text.


    Validation Server Controls Validation Server Controls