Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  How To Validate ASP.NET Pages
 »  Home  »  Web Development  »  ASP.NET  »  How To Validate ASP.NET Pages
How To Validate ASP.NET Pages
by Manjushree Das | Published  10/17/2006 | .NET Newbie ASP.NET | Rating:
Manjushree Das
You can reach me at manjushree.das@gmail.com  

View all articles by Manjushree Das...
Do we need validation?

 Say you have a requirement where you need to multiply two numbers so here you have to make sure that they are numbers.

How to validate?

A simple approach is to use if statements or a series of if statements that would check for data input satisfying a particular condition. By this method we can check for valid ranges of dates, blank entries and a host of other simple error prone issues.

Another way is to take the benefit of validation controls provided by ASP.NET.Validation in ASP.NET can be carried out both on the client side as well as the server side. These validation controls belong to the System.Web.UI.WebControls namespace.

Syntax:

<?xml:namespace prefix = asp /><asp:ValidationName ErrorMessage="DescriptiveText" ControlToValidate="ControlName" runat="server"></asp:ValidationName>

e.g
     
<asp:RequiredFieldValidator ErrorMessage="Enter a value. You cannot leave this field blank” ControlToValidate=" runat="server" ? txtName?>

Types of validation control in ASP.NET

RequiredFieldValidator :This control helps to prevent user from skipping a field that is required.
CompareValidator: This control helps to compare a user’s entry to that of a constant value.
RangeValidator: This control helps to check whether a user’s entry is between specified lower and upper boundaries.
RegularExpressionValidator :This control helps to check whether a user’s entry matches a pattern defined by a regular expression.
CustomValidator :This control helps to check the user’s entry based on the logic that you code yourself.


The set of validation controls described above are going to act on the following controls:

• HtmlInputText
• HtmlTextArea
• HtmlSelect
• HtmlInputFile
• TextBox
• ListBox
• DropDownList
• RadioButtonList

How Validation Controls Work

The working of validation controls can be explained by steps mentioned below:
1)HTML output is sent to client
2)The user enters input
3)Client side script are executed to determine if fields are valid
4)Check if all controls are valid or not
5)If valid then post data else display error message
6)Finally evaluate the validation controls


Custom validation controls

You can also create your own validation control to suit your need. The syntax is similar to the other validation control.

Syntax:

<asp:CustomValidator controltovalidate="phone" runat="server" left? Errormessage="The phone number must be 7 characters”>
</asp: CustomValidator>


Summary

Validation Controls are a must when you need to build any serious application. Validation controls are very beneficial and quite easier to use . Unlike the traditional programming language it saves lot of time of the programmer from writing complex code. But they have side effects too as they add some overhead to your pages, increasing files sizes and thus decreasing performance. Also for client side validation, the client has to support Javascript and DHTML.

</asp:CustomValidator></asp:RequiredFieldValidator>

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.38888888888888 out of 5
 36 people have rated this page
Article Score9808
Sponsored Links