ASP.NET Validations Tips and Tricks Part-II

Sheonarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 5263 red flag
Rating: 5 out of 5  
 1 vote(s)

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 !


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
http://www.snarayan.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)