Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

What are the ASP.NET Validation Controls and how to use them for client side validation

Author: Usman Lateef

Most of the times you need to check user inputs in a web page for example you don’t want any missing field in your form or you don’t want any invalid data from user.

ASP.NET validation controls give you a powerful way to check any missing field, to compare any value (Integer, string etc) or to check invalid data. Another advantage of ASP.NET validation controls is that your user will not need to wait for full page post back to occur. These ASP.NET controls can also be used for sever side validation but in this article you will learn client side validation. 

Following six validation controls are available in ASP.NET
  1. RequiredFieldValidator
    Make sure that user doesn’t miss an input field and user entered a value. 
  2. CompareValidator
    Allows comparison between user input and another item using comparison operators (equals to, less than, greater than). It Compare string, integer, double, date and currency.
  3. RangeValidator
    It ensures that user input value is within certain range defined as maximum and minimum values in string, integer, double, date and currency
  4. RegularExpressionValidator
    It checks the user entry by matching a pattern defined in the regular expression. RegularExpressionValidator is used to check email addresses and phone numbers.
  5. CustomValidator
    It allows you to write custom validation functions to check user input.
  6. ValidationSummary
    Displays a report of all error messages occurred in a web page


Follow the below example to use validation controls for client side validation

  1. Create a new empty website in visual studio 2010 either in C# or in VB.NET
  2. Add a new item (web form) in the website
  3. Copy and paste following code in the .aspx source page or drag and drop four TextBoxes, one Button and one Label from Standard Tab in Toolbox and one RequiredFieldValidator, one RegularExpressionValidator, one CompareValidator, one RangeValidator and One ValidationSummary  from Validation Tab and set their properties in design view of .aspx page 



Your Name:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

ControlToValidate="TextBox1" Display="Dynamic"

ErrorMessage="Required Field Validator" SetFocusOnError="True">Required Field Validator </asp:RequiredFieldValidator>

Your Email:

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

ControlToValidate="TextBox2" Display="Dynamic"

ErrorMessage="Regular Expression Validator" SetFocusOnError="True"

ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Regular Expression Validator </asp:RegularExpressionValidator>

Retype Email:

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

<asp:CompareValidator ID="CompareValidator1" runat="server"

ControlToCompare="TextBox2" ControlToValidate="TextBox3" Display="Dynamic"

ErrorMessage="Compare Validator">Compare Validator </asp:CompareValidator>

Your Age:

<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

<asp:RangeValidator ID="RangeValidator1" runat="server"

ControlToValidate="TextBox4" Display="Dynamic" ErrorMessage="Range Validator"

MaximumValue="50" MinimumValue="18" SetFocusOnError="True" Type="Integer">Range Validator </asp:RangeValidator>

<asp:Button ID="Button1" runat="server" Text="Button" />

<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>




Add following button click event in code behind file

protected void Button1_Click(object sender, EventArgs e) {
   Label1.Text = "Your Name is: " + TextBox1.Text + ", Your Email is: " + TextBox2.Text + ", Your Age is: " + TextBox4.Text;
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   Label1.Text = "Your Name is: " & TextBox1.Text & ", Your Email is: " & TextBox2.Text & ", Your Age is: " & TextBox4.Text
End Sub
Conclusion: ASP.NET Validation Controls are very easy to implement for client side validation and it guarantees that user inputs are complete and valid.


Add Article Comment:
Name :
Email Address :
Comments :
<< Handling events for programmatically created WebControls in ASP.NET

Disclaimer - Privacy
© 2002-2017