Checking the range using Range Validator Control in ASP.Net
Author: Faraz
In this article I will show you, how you can make sure that the date entered by visitors’ is within the specified range.
On some of your ASP.Net forms you may have fields in which visitors must enter a date and that date must fall within a specified range. For example, a birth date field would have a range that the date should fall between, as would a date on an invoice. You can use the Range Validator control to make sure the data entered by a visitor is a date within a specified range.
This technique will show you how to do that. This page displays a TextBox control, where visitor can enter the date between 1/1/1900 to 12/31/2000. If date is outside the range an error message is displayed.
The code for date validation is as follows:
Note: The things that you should take care of
- Set the ControlValidate property to the TextBox control.
- For date validation you have to set the Type property to Date. Set its range by selecting the Minimum Value and Maximum Value property.
- Set the Error Message property if the TextBox violates the rule.
- If you want to check the null strings too use the RequiredFieldValidator control.