Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
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:

 

<asp:Label ID="lblDisp" Runat="server" text="Your Valid Range is 1/1/1900 to 12/31/2000" Font-Bold="True" Font-Size="10pt" Font-Name="Verdana" />

<br><br>

 

<asp:TextBox id="txtValDate" runat="server" />

 

<asp:RangeValidator id="valDateRange" runat="server" ErrorMessage=" * Insert the Date Value Between the Valid Range " Font-Names="Verdana" Font-Size="XX-Small" Font-Bold="True" ControlToValidate="txtValDate" MaximumValue="12/31/2000" MinimumValue="1/1/1900"  Type =Date Display="Dynamic" /><br><br>

 

<asp:Button ID="cmdOk" Text="OK" Runat="server" OnClick="cmdOk_Click" />

 

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.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Templates in a Repeater control Using ASP.Net and Visual Basic.Net

Disclaimer - Privacy
© 2002-2010 DevASP.net