Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » WebForms Controls » Validation Server Controls in Web Forms

Validation Server Controls in Web Forms

When working with Web applications, developers need to write two separate codes using Java Script and VB Script to validate user inputs.In this article we are going to see, how we can reduce the programmers effort on coding, reducing the execution process time and simplify the code handling and less number of lines in the coding.

Page Views : 9628
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Article Description

When working with Web applications, developers need to write two separate codes using Java Script and VB Script to validate user inputs. Using two separate codes is not only a resource consuming (time and money) process, it also degrades the performance of applications. In this article we are going to see, how we can reduce the programmers effort on coding, reducing the execution process time and simplify the code handling and less number of lines in the coding.

Validation Server Controls

In ASP.NET the following server controls are available to validate the user inputs with less hazels in the coding.

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

CompareValidator Control

Syntax:

<
asp:CompareValidator
id="ProgrammaticID"
ControlToValidate="Programmatic ID of Server Control to Validate"
ValueToCompare="value"ControlToCompare="value" Type="DataType" Operator="Operator Value" ErrorMessage="Message to display in ValidationSummary control"Text="Message to display in control" ForeColor="value"BackColor="value" runat="server" >
</asp:CompareValidator>

In this control we can validate and compare two input variables based on their data type using the comparison operators like equal to, not equal to, greater than, les than etc. We can use this validation control in password verification, compare the user input with the value from the relational database.

CustomValidator Control

Syntax: <asp:CustomValidator id="ProgrammaticID" ControlToValidate="programmatic ID of Server Control to Validate"
ClientValidationFunction="ClientValidateID"OnServerValidate="ServerValidateID"
ErrorMessage="Message to display in ValidationSummary control"
Text="Message to display in control" ForeColor="value" BackColor="value" runat="server" >
</
asp:CustomValidator>

In this server control, we can validate the user input passes to the customized logic. This logic may vary depends upon the requirements. For example: In on-line banking the amount transfer transferred by the user is valid against his available balance (it should be less than his balance. If it is not, then ErrorMessage text will be displayed on your web form)

RangeValidator Control

Syntax:
<asp:RangeValidator id="ProgrammaticID"
ControlToValidate="ProgrammaticID of control to validate" MinimumValue="value"
MaximumValue="value" Type="DataType" ErrorMessage="Message to display in ValidationSummary control"Text="Message to display in control"ForeColor="value" BackColor="value" runat="server" >
</
asp:RangeValidator>

In this server control, we can check the number range between maximum and minimum values against the user input. For example: Age, year validation.

RegularExpressionValidator Control

Syntax:
<asp:RegularExpressionValidator id="ProgrammaticID" ControlToValidate="ProgrammaticID of control to validate" ValidationExpression="expression" ErrorMessage="Message to display in ValidationSummary control"Text="Message to display in control"ForeColor="value" BackColor="value"runat="server" >
</
asp: RegularExpressionValidator>

In this server control, we can validate the postcode, telephone number etc by using the regular expressions. These expressions may have special characters like matching patterns in the Unix commands.

RequiredFieldValidator Control

Syntax:
<asp:RequiredFieldValidator id="ProgrammaticID" ControlToValidate="ProgrammaticID of control to validate" InitialValue="value"ErrorMessage="Message to display in ValidationSummary control"Text="Message to display in control"ForeColor="value" BackColor="value" runat="server" >
</
asp:RequiredFieldValidator>

In this server control, we can validate the mandatory field validations in the user account details. If the mandatory field is blank or null the error message will be displayed in the web form.

ValidationSummary Control

Syntax:
<asp:ValidationSummary id="programmaticID"
DisplayMode="BulletList | List | SingleParagraph" EnableClientScript="true | false"
ShowSummary="true | false"ShowMessageBox="true | false" HeaderText="TextToDisplayAsSummaryTitle"runat="server"/>
</asp:ValidationSummary >

In this server control, it will summarize the validation results on the web form and will displayed with the bulleted list.

The below example shows that in User Account Details web form using the above validation server controls to validate user input details.

Source Code:
<%@ Page Language="C#" %>
<html>
<
head>
<
script runat="server">
void ValidateBtn_Click(Object sender, EventArgs e)
{
if (Page.IsValid)
{
zipOutput.Text = "Page is Valid!";
}
else
{
zipOutput.Text = "Page is InValid!";
}
}
</script>
</
head>
<
body>
<
h3>User Account Details</h3>
<
p>
<
form runat="server" ID="Form2">
<
table cellpadding="10">
<
tr>
<
td>
<
table bgcolor="#eeeeee" cellpadding="10">
<
tr>
<
td colspan="3">
<
asp:Label id="zipOutput" Text="Enter a 5 digit zip code" Font-Name="Verdana" Font-Size="10pt" runat="server"/>
</
td>
<
td>
<
asp:TextBox id="TextBox1" runat="server"/>
</
td>
<
td>
<
asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="TextBox1"ValidationExpression="\d{5}"Display="Static"
ErrorMessage="Zip code must be 5 numeric digits"runat="server"/>
</
td>
</
tr>
<
tr>
<
td colspan="3">
<
b>Age:</b>
</
td>
<
td>
<
asp:TextBox id="TextBox2" runat="server"/> </td>
<
td>
<
asp:RangeValidator id="Range1"ControlToValidate="TextBox2"MinimumValue="20"
MaximumValue="30"Type="Integer"Text="The value must be from 20 to 30!"
runat="server"/>
</
td>
</
tr>
<
tr>
<
td colspan="3">
<
b>Credit Card Information</b>
</
td>
</
tr>
<
tr>
<
td align="right">
Card Type:
</td>
<
td>
<
asp:RadioButtonList id="RadioButtonList1" RepeatLayout="Flow"runat=server>
<
asp:ListItem>MasterCard</asp:ListItem>
<
asp:ListItem>Visa</asp:ListItem>
</
asp:RadioButtonList>
</
td>
<
td align="middle" rowspan="1">
<
asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="RadioButtonList1"ErrorMessage="Card Type."Display="Static"
InitialValue="" Width="100%" Text="*"runat="server"/>
</
td>
</
tr>
<
tr>
<
td align="right">
Card Number:
</td>
<
td>
<
asp:TextBox id="TextBox3" runat="server" />
</
td>
<
td>
<
asp:RequiredFieldValidator id="RequiredFieldValidator2"
ControlToValidate="TextBox3" ErrorMessage="Card Number. "Display="Static"
Width="100%"Text="*" runat=server/>
</
td>
</
tr>
<
tr>
<
td></td>
<
td>
<
asp:Button id="Button1" Text="Validate"OnClick="ValidateBtn_Click"
runat
=server />
</
td>
<
td></td>
</
tr>
</
table>
</
td>
<
td valign=top>
<
table cellpadding="20">
<
tr>
<
td>
<
asp:ValidationSummary id="valSum" DisplayMode="BulletList"HeaderText="You
must enter a value in the following fields:"
runat="server"/>
</
td>
</
tr>
</
table>
</
td>
</
tr>
</
table>
</
form>
</
body>
</
html>

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Chandrasekaran Sakkarai
Chandrasekaran Sakkarai has been working as an Analyst Programmer using Microsoft and Java Technologies in various platforms. Currently he is working in .NET platform at Danoz Direct Pty Ltd, Sydney, Australia.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.