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”
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>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Text="Phone Number"></asp:Label>
<asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label3" runat="server" Text="Address "></asp:Label><br />
<asp:TextBox ID="txtaddress" runat="server" Height="128px" Width="160px"></asp:TextBox><br />
<br />
<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>
<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>
<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 />
<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 />
<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