asp.net.ph

Skip Navigation Links

Validating Against a Specific Value

ASP.NET Web Forms   Web Forms Server Controls   Web Forms Validation


You can validate a user’s entry against a specific single value using logical operators. For example, you can specify that the user’s entry is a date after January 1, 1950 or that it is an integer value greater than or equal to zero. Alternatively, you can specify that the user’s entry be compared against a value from another control.

To validate against a specific value

  1. Add a CompareValidator control to the page and set the following properties:

Property Setting
ControlToValidate The ID of the input control that the validation control will evaluate.
Display The display behavior for the specified validation control.
ErrorMessage The error message to display in the ValidationSummary control if validation fails.
Text The error message to display in the validation control when validation fails.
  1. Set the value to compare to by setting the following properties:

Property Setting
ValueToCompare or ControlToCompare An expression entered as a string. To compare to a constant value, set the ValueToCompare property. To compare against the value of another control, set the ControlToCompare property to the ID of that control ( the CompareValidator control compares the user’s entry against whatever property is specified by the other control’s ValidationPropertyAttribute. If you set both ValueToCompare and ControlToCompare, ControlToCompare takes precedence.
Type The data type of the two values to be compared. Types are specified using the ValidationDataType enumeration, which allows you to use the type names String, Integer, Double, Date, or Currency. The values are converted to this type before the comparison is performed.
Operator The comparison to use. Operators are specified using the ValidationCompareOperator enumeration, which allows you to enter the name of the comparison operators, such as Equal, NotEqual, GreaterThan, GreaterThanEqual, and so on.

NOTE: When validation is done against another control, invalid values in the other control are ignored and the validation passes. For details, see Special-Case Validation Results.

  1. In your Web Forms code, check for validity. For details, see Testing Validity Programmatically.

NOTE: If the user leaves a control blank, the control passes the comparison validation. To force the user to enter a value, add a RequiredFieldValidator as well. For details, see Validating Required Entries.

The following example shows a CompareValidator that compares the user’s entry against the value in another control. In a form that allows users to make reservations at a hotel, the validator checks that the user does not enter a departure date earlier than the arrival date. In a real application, the departure date would be required and validated as a date as well.

<asp:textbox id="txtDepartureDate" runat="server" />

<asp:comparevalidator id="compValidator" runat="server"
   forecolor="navy"
   controltovalidate="txtDepartureDate"
   controltocompare="txtArrivalDate"
   type="DateTime"
   operator="GreaterThanEqual"
   errormessage="Departure date cannot be earlier than arrival date." />
See Also

Validating Against a Data Type   Validating Against Values in a Database   CompareValidator Control   CompareValidator Class   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