asp.net.ph

Skip Navigation Links

CompareValidator Control

ASP.NET Syntax   ASP.NET Syntax for Validation Controls


Evaluates the value of an input control against a given value.

Syntax

<asp:CompareValidator id="accessID"
   ControlToValidate = "ID of input control to validate"
   ValueToCompare = "constant value or the value of another control"
   ControlToCompare = "the name of the control to compare"
   Type = "datatype to convert to before comparing"
   Operator = "the type of comparison to perform 
      ( less than, greater than, equal to, etc. )"
   ErrorMessage = "message to display in ValidationSummary control"
   Text = "message to display in control"
   ForeColor = "color value"
   BackColor = "color value"
   runat="server" />

For information on the individual members of this class, see CompareValidator in the class library.

Remarks

The CompareValidator control allows you to compare a value against another, to determine if the values match the relationship specified by a comparison operator ( less than, equal to, greater than, etc. ).

The value to compare comes from user input, typically entered by the user into an input control. The value to compare against may be a given constant value, the value of another control, or a value extracted from an external data source, such as the value of a field in a database.

You can also use the CompareValidator control to determine whether the value entered into an input control can be converted to the data type specified by the Type property.

Specify the input control to validate by setting the ControlToValidate property. If you want to compare a specific input control with another input control, set the ControlToCompare property with the name of the control to compare.

Instead of comparing the values of two input controls, you can compare the value of an input control to a constant value. Specify the constant value to compare with by setting the ValueToCompare property.

The Operator property allows you to specify the type of comparison to perform, such as greater than, equal to, and so on. If you set the Operator property to DataTypeCheck, the CompareValidator control ignores both the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered into the input control can be converted to the data type specified by the Type property.

NOTE: If the value of the control to validate is empty, no validation functions are called and validation succeeds. To prevent the user from skipping an input control, use a RequiredFieldValidator.

Example

The following example demonstrates how to use the CompareValidator to check whether the value entered in a text box matches the value entered in another.

 Show me 

See Also

CompareValidator Class   Validating Against a Specific Value   Validating Against a Data Type   Validating Against Values in a Database   Data Entry Validation ( Client-Side )



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note