Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3979 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET AJAX > How to use Validation Controls in ASP.NET

How to use Validation Controls in ASP.NET

2 vote(s)
Rating: 4 out of 5
Article posted by Vuyiswamb on 2/11/2010 | Views: 24313 | Category: ASP.NET AJAX | Level: Beginner red flag


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


 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

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.

Experience:8 year(s)
Home page:http://www.Dotnetfunda.com
Member since:Sunday, July 06, 2008
Level:NotApplicable
Status: [Member] [MVP] [Administrator]
Biography:Vuyiswa Junius Maseko is a programmer and a moderator in ".NetFunda. Vuyiswa has been developing for 8 years now. his major strength are C# 1.1,2.0,3.0,3.5 and sql and his interest are in Silverlight,WPF,C#. He has been doing a lot of Silverlight development. He has been using .net since the beta version of it. He is also an online Trainer at www.Itfunda.com. Thanks to people like Sheo Narayan (.Netfunda) , Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject),.They have made vuyiswa what he is today.
 Responses
Posted by: Abhijit Jana | Posted on: 23 Feb 2010 01:40:50 PM

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.

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

How to call code behind method in JavaScript in C# and VS2010

CalendarExtender is an ASP.NET AJAX control that is generally used with TextBox control. When user clicks in Textbox, Calendar control Pops up. Here I am discussing various properties of CalendarExtender .

In this example I create a validation control. It checks if the TextBox is Empty it shows the error message.

Windows applications are more stable than web application. Even if the web is not that stable like a windows application, there are lot advantages of building Web Applications and one thing that we remember as ex-Windows programmers is that the Forms did not Flicker in front of the eyes of the user. This irritates a lot of users. Microsoft came back with a good solution for this problem and they introduced Ajax. In this Article am going to show you how you can create Flicker Free pages.

This article explains how to create Gmail like loading indicator.

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 found 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. | 5/21/2012 7:45:06 AM