How to use Validation Controls in ASP.NET

Vuyiswamb
Posted by in ASP.NET AJAX category on for Beginner level | Views : 52676 red flag
Rating: 4.33 out of 5  
 3 vote(s)

Using Validation Controls is faster than doing validation on the server side. Please note that there are some cases where you can’t set a rule. In that Case you will add those types of validation to your Business Logic Layer, when you validate in your Class Library before sending the Data to the Data Layer to be executed. There is only one reason why Microsoft created Validation controls. The Reason is simple “Never Trust the user input”


 Download source code for How to use Validation Controls in ASP.NET

Introduction

Most of the Recent Articles I wrote were short articles written to expose the basic info on certain subject. Most of Programmers who started to code windows applications, find it hard to adapt to the style used in the web. Most of the code they write in web application is on the server side not on the client side. In a Windows application the application takes the advantage of the client resources, but on the web application it’s an entirely different scenario. A Programmer needs to choose programmatically when reply on, Server or Client side. In this Article am going to Explain on how to validate the user input using the validation controls.

Where to get the Validation Controls

Create a new website project and you have an Empty default.aspx page. In your Solution Explorer double click it and go to its design view. Look for your toolbar on the left hand side of your window. In the Toolbar look for the Validation Section as depicted below



Start
Now leave this for now. Let us add at least seven controls as I did here


And the mark-up will look like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Text="Phone Number"></asp:Label>
&nbsp; &nbsp;
<asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label3" runat="server" Text="Address "></asp:Label><br />

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<asp:TextBox ID="txtaddress" runat="server" Height="128px" Width="160px"></asp:TextBox><br />
<br />

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" /></div>
</form>
</body>
</html>
Now as you see there is no Validation to the page. But how can we add validation without writing the server validation code. Let us look at our Validation controls in the previously visited Toolbar section.



  • The Required Field is use when you want to make sure that there should be a value before the page is submitted to the server.
  • RangeValidator is used when you want to validate from one range to another. It has properties, “MaximumValue” and “Minimunvalue”; those are the Properties you can use.
  • RegularExpressionValiadator is used to restrict the range of valid characters, to strip unwanted characters, and to perform length and format checks.
  • CompareValidator is used to compare two values if they are the same. This is used mostly in password and password repeat scenario.
  • ValidationSummary is used to display all the Warning for all controls that are linked to the validation control.
  • We cannot test all of them; we will just use some of them. Change your mark-up and add the validation control so that it will look like this

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname"
ErrorMessage="* Please enter your name">* Please enter your name</asp:RequiredFieldValidator><br />
<br />
<asp:Label ID="Label2" runat="server" Text="Phone Number"></asp:Label>
&nbsp; &nbsp;
<asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtPhoneNumber"

ErrorMessage="* Please enter your Phone Number">* Please enter your
Phone Number</asp:RequiredFieldValidator><br />
<br />
<asp:Label ID="Label3" runat="server" Text="Address "></asp:Label><br />

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<asp:TextBox ID="txtaddress" runat="server" Height="128px" Width="151px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPhoneNumber"

ErrorMessage="* Please enter your Address">* Please enter your
Address</asp:RequiredFieldValidator><br />
<br />

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Button ID="btnSubmit" runat="server" OnClick="Button1_Click" Text="Submit" Width="68px" /><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True" />
</div>
</form>
</body>
</html>

The Design should look like this



The Property Text in these Controls is used for displaying the message on individual validation control but there is another property named “ErrorMessage”, this Property is used for messages that will appear in the ValidationSummary control.  The last Part Run the Application and Press the Button without Adding anything to any field and you will see something like this


You will Notice that after you enter a value on one field after leaving that field to another the message disappears. This is faster than when try to validate yourself on the server side.

Conclusion

Using Validation Controls is faster than doing validation on the server side. Please note that there are some cases where you can’t set a rule. In that Case you will add those types of validation to your Business Logic Layer, when you validate in your Class Library before sending the Data to the Data Layer to be executed. There is only one reason why Microsoft created Validation controls. The Reason is simple “Never Trust the user input”


Vuyiswa Maseko
Page copy protected against web site content infringement by Copyscape

About the Author

Vuyiswamb
Full Name: Vuyiswa Maseko
Member Level: NotApplicable
Member Status: Member,MVP,Administrator
Member Since: 7/6/2008 11:50:44 PM
Country: South Africa
Thank you for posting at Dotnetfunda [Administrator]
http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.

Login to vote for this post.

Comments or Responses

Posted by: Abhijit Jana on: 2/23/2010
Vuyiswa, Good Job !

I would like to suggest you to add few point for server side validation. Means if you are using ASP.NET client side validation, user can disable the JAvascript in browser and can easily bypass the validation, on that we can use Page.IsValide() to ensure all valdation done or not.
Hope you got my point.

Thanks again for sharing a great article.
Posted by: Siddu1281 on: 8/17/2012 | Points: 25
hey it is very informative to me
thanks

Login to post response

Comment using Facebook(Author doesn't get notification)