Export (0) Print
Expand All

Validating Against a Data Type for ASP.NET Server Controls

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:
    ControlToValidateThe ID of the control for which you are checking the data type.
    ErrorMessage, Text, DisplayProperties that specify the text and location of the error or errors that will display if the validation fails. For details, see Controlling Validation Error Message Display for ASP.NET Server Controls.
  2. Set the data type to compare to by setting the following properties:
    TypeThe 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.
    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 control as well. For details, see Validating Required Entries for ASP.NET Server Controls.
  3. Add a test in your Web Forms code to check for validity. For details, see Testing Validity Programmatically for ASP.NET Server Controls.

See Also

Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display for ASP.NET Server Controls

© 2014 Microsoft