How to validate a textbox for range of values,correct date?

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 17385 red flag

In scenarios where we want to validate a textbox for the specific range of values, and ensure that the correct Date to be entered, we can follow this approach.

Validations

Validation controls are used to validate the form fields for valid data when it is submitted to the server.

In the previous article, we learnt about What is "CauseValidation" property and how to avoid validation on clicking of a button? How to validate two different sets of Form fields separately? In this article, we shall How to validate a textbox for range of values,correct date and How to validate a valid email address entered into the textbox.

Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.

In case we want to validate a textbox for the specific range of values to be entered, we can follow this approach.

ASPX PAGE

Enter year between 2000 to 2010:

<asp:TextBox ID="txtRange" runat="server" /><br />

<asp:RangeValidator ID="range1" runat="server" ErrorMessage="Sorry, must be

between 2000 and 2010" ControlToValidate="txtRange" MaximumValue="2010" MinimumValue="2000"

Type="Integer" ForeColor="Red" />

<p>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

</p> 

In the above code snippet, we have a TextBox that must accept the value in between 2000 and 2010. To validate this range, we can use asp:RangeValidator with MaximumValue as 2010 and MinimumValue as 2000 with Type as “Integer”. This causes the TextBox to accept only values in between 2000 to 2010.

Note: The asp:RangeValitor only works if any data is entered to the TextBox, if no data is entered and button is clicked the form is submitted to the server without validation. To avoid this, we should keep asp:RequiredFieldValidator attached with the TextBox to validate.

OUTPUT

Note: Similarly Date, Double, String, Currency values can be validated for specific range.

In case we want to validate a textbox value for the correct date, we can follow this approach.

ASPX PAGE

Date of birth:

<asp:TextBox ID="txtDOB" runat="server" /><br />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ControlToValidate="txtDOB"

ForeColor="Red" />

<asp:CompareValidator ID="comp1" runat="server" ErrorMessage="Date must be in

MM/DD/YYYY format" ControlToValidate="txtDOB" Type="Date" Operator="DataTypeCheck" ForeColor="Red" />

<p>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

</p> 

In the above code snippet, we have a TextBox that must accept correct date. To do that we can use asp:CompareValidator with Operator as “DataTypeCheck” and Type as “Date”. We have used asp:RequiredFieldValidator to force user to enter some data into the TextBox before the form is submitted to the server.

OUTPUT

In scenarios where we want to check whether a valid email id has been entered into the TextBox or not, we can follow this approach.

ASPX PAGE

Your email:

<asp:TextBox ID="txtEmail" runat="server" Columns="50" />

<br />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ControlToValidate="txtEmail"

ForeColor="Red" />

<asp:RegularExpressionValidator ID="ReExp1" runat="server" ControlToValidate="txtEmail"

ErrorMessage="eg. abc@domain.com" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-

.]\w+)*"></asp:RegularExpressionValidator>

<p>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" />

</p> 

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{

 

}

 

In the above code snippet, we want to validate the TextBox for correct format of the email id. For that we have used asp:RegularExpressionValidator with ValidationExpression as “\w+([-+.']\w+)*@\w+([- .]\w+)*\.\w+([-.]\w+)*” that validates the TextBox data for correct email id format (eg. abc@domain.com).

We have used asp:RequiredFieldValidtor to force user to enter some data into the TextBox.

OUTPUT

Hope this article was useful. Thanks for reading.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)