|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Validate Against a Data Type for ASP.NET Server Controls
You can validate a user's entry in an ASP.NET Web page 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 acontrol to make sure that the date is in a recognized format before it is submitted.
To validate against a data type
Add a CompareValidator control to the page and set the following properties:
The ID of the control for which you are checking the data type.
Properties that specify the text and location of the error or errors that will display if the validation fails. For details, see.
Set the data type to compare to by setting the following properties:
The data type to check against. The type is specified using theenumeration, which enables you to use the type names , , , , or .
If the user leaves a control blank, the control passes the comparison validation. To force the user to enter a value, add acontrol as well. For details, see .
Add a test in your ASP.NET Web page code to check for validity. For details, see How to: Test Validity Programmatically for ASP.NET Server Controls.