Click here to Skip to main content
13,297,048 members (64,093 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads


123 bookmarked
Posted 21 Mar 2010

Exploring ASP.NET Validators

, 9 Apr 2010
This article digs deep into ASP.NET validators and discusses a few case studies which may be a boon for any project
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CaseStudy1.aspx.cs" Inherits="CaseStudy1" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
        <script type="text/javascript" language="javascript">
    var curobjid;
    var i=0;
    var isValid = true;
    //This function is going to be caled on the onfocus event of the textbox
    function AttachValidator(curObj, validatorClientID)
        // isValid Check:If user enters some wrong value, then he will not ne allowed to some value in another textbox till he enters right value.
            var validationControl = document.getElementById(validatorClientID);
            //Assigning controltovalidate properly to specific textbox dynamicaaly
            validationControl.controltovalidate =;
            //Here also I am assigning the clientvalidationfunction.we can also change it dynamically based on the requirement. 
            validationControl.validateemptytext = "true"; 
            //This is the main function that is responsible for attaching th validator to the correspondng textbox 
            ValidatorHookupControl(curObj, validationControl);
            //keeping the textbox id in a global varable that is going to be used to show the callout
    //This is Client validation function
    function validatetextbox(sender, args)
            sender.errormessage="<b>Required Field Missing</b><br />This is required." ;
            args.IsValid = isValid = false;         
        else if(isNaN(args.Value))
            sender.errormessage="<b>Invalid field</b><br />Only Numbers are alowed." ;   
            args.IsValid = isValid = false;
        else if(Number(args.Value)< 1000)
            sender.errormessage="<b>Value can not be less than 1000</b><br />This is required." ;  
            args.IsValid = isValid = false;      
            isValid = true;
            ShowValidationCallout(sender, curobjid);
 //This function is going update the postion of the callout so it gets visible at the proper place.    
function ShowValidationCallout(validationControl, elementClientID)
        if(validationControl.ValidatorCalloutBehavior != null)
            if(validationControl.ValidatorCalloutBehavior._popupBehavior != null)
            if(validationControl.ValidatorCalloutBehavior._errorMessageCell !=null)
                validationControl.ValidatorCalloutBehavior._errorMessageCell.innerHTML = validationControl.errormessage;
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server">
    <table border="1px" width="50%" style="font-family:Verdana">
            <td width="50%" style="font-weight:bolder">
            <td width="50%" style="font-weight:bolder">
        <asp:Repeater ID="rpUpdateAdpdetails" runat="server" OnItemDataBound="rpUpdateAdpdetails_ItemDataBound">
                <tr style="border-top: 1px solid #E8E8E8">
                    <td style="border-top: 1px solid #E8E8E8">
                        <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
                    <td style="border-top: 1px solid #E8E8E8">
                        <!--This is the textbox on the grid validator will be attached -->
                        $<asp:TextBox ID="txtCost" runat="server" Width="50px"></asp:TextBox>
     <!-- This custom validator is going to attached dynamically on all the texboxes-->
        <asp:CustomValidator ID="gridValidator" runat="server" ></asp:CustomValidator>
           <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" TargetControlID="gridValidator">
        <%--<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
          <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" TargetControlID="RequiredFieldValidator1">
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" ControlToValidate="TextBox1" ClientValidationFunction="validatetextbox"></asp:CustomValidator>
         <cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" TargetControlID="CustomValidator1">
        <asp:Button ID="Button1" runat="server" Text="Button" />--%>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 10 Apr 2010
Article Copyright 2010 by Brij
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid