DevCity.NET -
How To Validate ASP.NET Pages
Manjushree Das
You can reach me at  
by Manjushree Das
Published on 10/17/2006

Validation is pretty much necessary in almost all kind of applications. The simple fact is that humans are prone to make mistakes while entering data. In order to build a fully fledged application which is free from errors and not susceptable to crashes we should pay attention to input validation. Input validation is the process of making sure that the user provides the accurate type of data because “prevention is better than cure”. If an application requires certain specific data and we feed another type of data then definitely the result is an error. So care must be taken to enter data in a particular format. This can be better explained with the help of an example.

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.


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

<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.


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


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.