Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 18614 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > ASP.NET Validations Tips and Tricks Part-II

ASP.NET Validations Tips and Tricks Part-II

1 vote(s)
Rating: 5 out of 5
Article posted by Sheonarayan on 7/18/2011 | Views: 3792 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag

Advertisements

Advertisements
In this article we shall learn how to validate two different sets of Form fields separately, how to validate a textbox for range of values and how to validate a textbox for correct date.

Introduction

Validation controls are used to validate the ASP.NET input server controls like asp:TextBox for valid data when their data is submitted to the server.

There are six validation controls in ASP.NET and they are

    1. RequiredFieldValidator
    2. RangeValidator
    3. CompareValidator
    4. RegularExpressionValidator
    5. CustomValidator and
    6. ValidationSummary

I have recently published ASP.NET, ADO.NET, jQuery, HTML, CSS and JavaScript How to Tips and Tricks, click here to get it.

Now lets learn Tips and Tricks related with ASP.NET Validation controls. 

How to validate two different sets of Form fields separately?

In case we have more than one html form on the page and we want to validate them separately or we have two sets of form elements and we want to validate them separately, we can follow this approach.

ASPX PAGE

<fieldset>

<legend><b>User Login</b></legend>

<p>

Username: <asp:TextBox ID="txtUserName" runat="server"

ValidationGroup="LoginForm1" />

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

!" ForeColor="Red" ControlToValidate="txtUserName" ValidationGroup="LoginForm1" />

</p>

<p>

Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" ValidationGroup="LoginForm1" />

<asp:RequiredFieldValidator ID="req2" runat="server" ErrorMessage="Mandatory

!" ForeColor="Red" ControlToValidate="txtPassword" ValidationGroup="LoginForm1" />

</p>

<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="SubmitData1" ValidationGroup="LoginForm1" />

</fieldset>




<fieldset>

<legend><b>Admin Login</b></legend>

<p>

Username: <asp:TextBox ID="txtAUserName" runat="server"

ValidationGroup="LoginForm2" />

<asp:RequiredFieldValidator ID="req3" runat="server" ErrorMessage="Mandatory

!" ForeColor="Red" ControlToValidate="txtAUserName" ValidationGroup="LoginForm2" />

</p>

<p>

Password:<asp:TextBox ID="txtAPassword" runat="server" TextMode="Password" ValidationGroup="LoginForm2" />

<asp:RequiredFieldValidator ID="req4" runat="server" ErrorMessage="Mandatory

!" ForeColor="Red" ControlToValidate="txtAPassword" ValidationGroup="LoginForm2" />

</p>

<asp:Button ID="btnASubmit" runat="server" Text="Login" OnClick="SubmitData2" ValidationGroup="LoginForm2" />

</fieldset>

 

CODE BEHIND

protected void SubmitData1(object sender, EventArgs e)

{

}


protected void SubmitData2(object sender, EventArgs e)

{

}

 

In the above code snippet, on .aspx page we have two sets of Login form (User Login and Admin Login) separately. To validate both forms separately we can distinguish their controls using ValidationGroup property. Notice that both forms asp.net server controls have “LoginForm1” and “LoginForm2” as ValidationGroup that causes the forms to be validated separately.

OUTPUT

How to validate a textbox for range of values?

In case we want to validate a texbox for the specific range of values, 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. 

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

OUTPUT

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

How to validate a textbox for correct date?

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. When user enter some data and that is not in the correct date format, he/she gets message as displayed in the picture below.

OUTPUT


In case you have missed the 1st article in this series, click here to read.

Hope you are liking the articles in this series. Do let me know if you have any question. 

Do not forget to subscribe for the RSS feed to get the new articles alert directly in your Inbox, follow on tweeter at @dotnetfunda to get new post alert !

Thanks for reading, keep learning and sharing knowledge !


Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

One of the most important components in a project is the DAL component. Many developers end up creating their own DAL component which is pretty time consuming. A Microsoft enterprise application block has a very decent and time tested DAL component i.e. the Enterprise data application blocks. This tutorial will run through the necessary steps of how you can use Enterprise data application blocks in your DAL component.

In this post I show you how can you get the maximum out of standard asp.net control

In this article we are going to see the fileupload without postback.. We are going to do this by using ICallbackEventHandler..

In this section we will touch base on one of important concepts in ASP. NET.

When we consume business objects in UI the logic can become pretty complex. Activities like setting the business object value, getting the object value and setting the user interface from the object value. We can minimize the complexity of UI interaction with business object using mediator pattern.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 8:32:34 PM