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



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”);

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s