Field Validations – ASP.NET

Date Text Field Validations

Requirement:Validate entering birth day in a text field with mm/dd/yyyy format.

code in the .aspx or in .ascx


<asp:TextBoxID=”dateOfBirthTextBox”runat=”server”></asp:TextBox>

<asp:RequiredFieldValidatorID=”dateOfBirthRequiredFieldValidator”runat=”server”

ControlToValidate=”dateOfBirthTextBox” ErrorMessage=”Required”></asp:RequiredFieldValidator>

<asp:RangeValidatorID=”dateOfBirthRangeValidator”runat=”server” ControlToValidate=”dateOfBirthTextBox”

ErrorMessage=”Please enter a past date in mm/dd/yyyy format”  Type=”Date”Display=”Dynamic”></asp:RangeValidator>

code in the .cs file – Under OnInit or PageLoad methods

this.dateOfBirthRangeValidator.MinimumValue = DateTime.MinValue.ToString(“MM/dd/yyyy”);
this.dateOfBirthRangeValidator.MaximumValue = DateTime.Today.ToString(“MM/dd/yyyy”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s