WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 51 users


WorldofASP.NET >> ASP.NET >> Unedited ASP.NET

ASP.NET Input Validation Controls Tutorial

This article covers basic tutorial on how to use validation controls in asp.net
Published Date : 09 Oct 2008
Author : Krutarth P Patel
Language :
Platform : .NET
Technology : ASP.NET
Views : 6463
Rating : (0 votes so far)



Introduction

The most tedious work to avoid incorrect piece of information in asp is to validate user control. There are number of things you need to written in asp code to handle such a un needed information. ASP.Net provides different types of validation control to handle such a incorrect information and make you development performance faster.

ASP.NET validation controls works two ways of validation.

  • Server Side
  • Client Side

Main

The client side validation avoid extra round trip to the server.
Server side validation check input during data posting on server.
client-side/server-side detection and validation is done without extra work by the developer.

With ASP.NET, support different types of validation control.
1. RequiredFieldValidation Control
2. CompareValidator Control
3. RangeValidatior Control.
4. RegularExpressionValidator Control.
5. CustomValidator Control.
6. ValidationSummary



Common Properties of Validation Control

1. Control to Validate :
The ID of the input control that the validation control will evaluate. If this is not a legitimate ID, an exception will thrown.

2. Display
The display behavior for the specified validation control. This property can be one of the following values:
   None : The validation control is never displayed inline. Use this option when you want to show the
               error   message only in a ValidationSummary control

   Static : The validation control displays an error message if validation fails. Space is allocated on the Web
                page for the error message even if the input control passes validation. The layout of the page 
                does not change when the validation control displays its error message. Because the page
                layout  is static, multiple validation controls for the same input control must occupy different
                physical locations on the page

   Dynamic : The validation control displays an error message if validation fails. Space for the error   
                     message  is allocated dynamically on the page when validation fails. This allows multiple
                     validation controls to share the same physical location on the page.

3. Error Message : The error message to display in the ValidationSummary control if control fails. If the Text property of the validation control is not set, this text is also displayed in the validation control  when validation fails. The ErrorMessage property is commonly used to provide different messages for the validation control and the ValidationSummary control.

4.Validation Group :Specifies the name of the validation group to which this validation control belongs.

The RequiredFieldValidation Control

As the name suggest RequiredFieldValidation Control use to validate control against the value or text of the control. it make sure that a user inputs a value
Username <asp:textbox id="txt_username" runat="server"/>
<asp:RequiredFieldValidator id="username_required" runat="server" ControlToValidate=" txt_username "    ErrorMessage="Enter username to sign in" Display="dynamic">
</asp:RequiredFieldValidator>


In this example, we have a textbox which will not be valid until the user types something in. Inside the validator tag, we have a single *. The text in the innerhtml will be shown in the controltovalidate if the control is not valid. It should be noted that the ErrorMessage attribute is not what is shown. The ErrorMessage tag is shown in the Validation Summary (see below).


As the screen layout if user do not enter anything into the username text box the validation control raised an error.

This validation control also support initial value property to avoid specific string to validate basically it is useful with dropdownlist control

The CompareValidator Control


This is the control which compare two controls on the form. When we need comparison between to controls value this validation control is used. Let we see how it works. The most common example to compare password and confirm password this control definetely helps you.
Password : <asp:textbox id="txt_password" runat="server"/>
<br />
Confirm Password: <asp:textbox id="txt_confirm_password" runat="server"/>
<br />
<asp:CompareValidator id="ComparePassword" runat="server"
ControlToValidate="txt_password" ControlToCompare="txt_confirm_password"
Operator="Equals"
ErrorMessage="Password and confirm password do not match"
Display="dynamic">
</asp:CompareValidator>

Here the layout indicate the password and confirm password do not have the same value. So when user press button the error message will displayed. At the specified space
Here we have a sample where the two textboxes must be equal. The tags that are unique to this control is the ControlToCompare attribute which is the control that will be compared. You can also use operator attributes to compare tow control like Operator attribute can contain Equal, GreaterThan, LessThanOrEqual, etc. We can also use CompareValidator differently to compare control with specific value. like
<asp:textbox id="txt_age" runat="server"/>
<asp:CompareValidator id="valage" runat="server" ControlToValidate="txt_age"    ValueToCompare="50"    Type="Integer"    Operator="GreaterThan"    ErrorMessage="* Enter age greater than 50" Display="dynamic">*
</asp:CompareValidator>
You can also use different data types like Currency, Double, Date, Integer or String.

The RangeValidator Control

Range validator control is use to check the enter control value is in specific range or not. There are tow important property of this control is MaximumValue and MinimumValue
Age : (20 – 40 )
<asp:textbox id="txt_age" runat="server"/>
<asp:RangeValidator id="valageRange" runat="server"    ControlToValidate="txt_age"    MaximumValue="40"    MinimumValue="20"    Type="Integer"    ErrorMessage="Age must be between 20 and 40" Display="static"></asp:RangeValidator>


As the give screen shot the range validation control will raise the error message when the entered age is not between the specified max and min value property As second screen layout there need to set the property of MaximumValue and MinimumValue

The RegularExpressionValidator Control

This is the validation control most widely used for different aspect like to chek email validation, pincode validation, url link validation etc. This is the control use to strictly follow the expression as programmer wants. This control also gives some basic validation expression as well as user can also make his own expression on this control
Email Address <asp:textbox id="txt_email" runat="server"/>
<asp:RegularExpressionValidator id="val_email" runat="server"    ControlToValidate="txt_email"    ValidationExpression=".*@.*\..*"    ErrorMessage="Enter valid email address."    display="dynamic">
</asp:RegularExpressionValidator>

As the example suggest user need to enter @ and “.” In to the email address. But as user enter “,” (comma) it will display an error message


As the above screen shot we can say there are different option available with the regular expression validation control you can change it as required. When you click on property validation expression in result Regular expression editor window will display where different expression are list. User can also make his own expression by clicking on (custom) option and in validation expression textbox user can enter his expression as needed.

The CustomValidator Control

Custom validation control gives flexibility to programmer to write his own validation code We have a custom validator where we get to write out own functions and pass the control value to this function
Field: <asp:textbox id="txt_date" runat="server">
<asp:CustomValidator id="valdate_dd_mm_yyyy” runat="server"    ControlToValidate="txt_date"    ClientValidationFunction="Check_Date_Client"    OnServerValidate="Check_Date_Server"    ErrorMessage="* Enter Date in dd-mm-yyyy formate" dispaly="dynamic">*
</asp:CustomValidator>
There are two properties named ClientValidationFunction and OnServerValidate. These are the tell the validation control which functions to pass the controltovalidate value to. ClientValidationFunction is usually a javascript function. And written within the <SCRIPT> tag.

OnServerValidate is the function that is server-side to check for validation if client does not support client-side validation.
Sub Check_Date_Server (objSource As Object, objArgs As
Check_Date_ServerEventsArgs)
' Code

End Sub

Validation Summary

Validation Summary Control is used to display the validation error messages in one place. Suppose you want to display all the validation messages in one page to make the page look better and cleaner than you can use Validation Summary Control. The validation summary control will collect all the error messages of all the non-valid controls and put them in a tidy list. The list can be either shown on the web page (as shown in the example above) or with a popup box (by specifying ShowMessageBox="True")

<asp:ValidationSummary id="val_Summary" runat="server"    HeaderText="Errors: on form"    ShowSummary="true" DisplayMode="List" />
Enjoy the Validation. Good luck



    Other Related and Popular Articles :

    Simple asp2 feedback form
    A simple way to get user feedback via webpage email.

    Basics of XHTML-MP
    Basics of XHTML-MP

    181
    181

    Custom Validation for DOB Using three dropdownlist
    This article is exactly usefull to validate date of Birth having three dropdownlist control.

    Repeater within Repeater
    this article is used to show hierarchical data using repeater control.

    Introduction to Data Binding in ASP.NET 2.0
    This article explains the basic concept about Data Binding in ASP.NET 2.0


    Author Profile : Krutarth P Patel

    Hello friends, my selt Krutarth P. Patel.
    I m working as software Engineer.
    currently i m working on ASP.Net Plateform.

    I always focus on, how to gain maximum productivity with very less effort. So, I look for generalized productive framework.
    I m always belive in teamwork.
    Teamwork is fuel that allows common people to achive uncommon result.
    And this sit gives a teamwork to get good outcome and easy to produce inovative things

    Click here to view Author Profile


    How would you rate the quality of this content?
    Poor Excellent

    Comments

    Leave New Comments


    Article Content copyright by Krutarth P Patel
    Everything else Copyright © by WorldofASP.NET 2010

    Category
    .NET 3.5
    AJAX and ATLAS
    ASP.NET
    C# Programming
    Classic ASP
    Enterprise Systems
    General .NET
    VB.NET Programming
    Announcements
    Earn Cash by writing an article or review
    For more info Click here







    Legend : - Within 3 Days - Within 6 Days - Within 9 Days

    Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
    © 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
    Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing