.NET Tip of The Day
Learn one new .NET trick every day
Login or Join
.NET Tips & Tricks Community RSS

Those who prefer reading this site through RSS now can also subscribe for updates to .NET Tips & Tricks Community.

A simple method to validate an integer, double, date, and currency values entered into a textbox on ASP.NET page

Normally the CompareValidator is used to check one value against another on ASP.NET page. However it also can be used to ensure user has entered data in a correct format.

Here's how it works:

  1. Set the ControlToValidate property to the ID of the TextBox.
  2. Set the Operator property to DataTypeCheck.
  3. Assign a ValidationDataType enumeration member to the Type property, which includes String, Integer, Double, Date, and Currency.

Note: this method is culturally sensitive. This means that if you change your browsers settings to en-UK, a date of 1-28-01 correctly fires the validator, while 28-1-01 is allowed through. This is because the date format is day-month-year in that region.

1/5/2008
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.