asp.net.ph

Skip Navigation Links

Validating Against a Data Type

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


You can validate a user’s entry against a specific data type to be sure that what the user has entered is a number, or a date, and so on. For example, if you want to collect birth date information on a user registration page, you can use a CompareValidator control to make sure that the date is in a recognized format before it is submitted to your database.

To validate against a data type

  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 data type to compare to by setting the following properties:

Property Setting
Type The data type to check against. The type is specified using the ValidationDataType enumeration, which allows you to use the type names String, Integer, Double, Date, or Currency.
Operator Set to DataTypeCheck.
  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.

See Also

Validating Against a Specific Value   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