Click here to Skip to main content
15,867,944 members
Articles / Web Development / ASP.NET

Validating Partial Page with ASP.NET Validators using JavaScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Mar 2011CPOL2 min read 12.2K   8   2
Validating partial page with ASP.NET Validators using JavaScript

Validation plays a key role whenever we take inputs from the user. For this, ASP.NET provides us with few validator controls that are used a lot in web applications. Nowadays, in our application, we have several sections in our web pages. And also, we populate some data based on the user’s input. So several times, it is required to validate the page partially not the entire page, while earlier we used to have a single submit button and there we needed to validate the page.

AJAX also played a key role, for partial post back, initiate a call to server, etc., which requires to validate a part of the page.

So, in these scenarios, our earlier approach of validation would not work. In this kind of scenario, I will discuss few things that will be very helpful.

First, I would suggest to divide your page in some logical section, those you might need to validate at certain points. Then, assign a different group name to the validators on those sections.

  1. If you are initiating an Ajax call or initiating a callback from JavaScript, and you want to validate certain sections/parts of the page.

    So before initiating the Ajax call, you need to validate the user inputs. This can be easily done with the following method.

    You need to call Page_ClientValidate() method, this method has two flavors:

    • Page_ClientValidate() it fires all the validators on the page and returns false, if it fails
    • Page_ClientValidate(‘groupname’), this method fires all the validators having the passed groupname and returns false if any validator fails

    This is quite useful, while validating partial section/part of the page. So your JavaScript code may look like this:

    JavaScript
    function SaveAddress()
    {
        if(Page_ClientValidate('groupname') == false)
            return false;
        else
        {
            //Save Address
        }
    }
  2. Several times, it happens that based on some requirement, we used to hide some section of input form or hide some input control, but whenever page/section is going to be submitted, the hidden validators also get fired. So to overcome this problem, you may need to disable the validators. So to disable any validator from JavaScript:
    JavaScript
    document.getElementById('ValidatorClientId').enabled=false;

    So this validator wont be fired till you enable it again. You can enable it as:

    JavaScript
    document.getElementById('ValidatorClientId').enabled=true;

Hope you like this post and find it useful.

License

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


Written By
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 www.asp.net.

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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»4-Apr-11 20:12
professionalKunal Chowdhury «IN»4-Apr-11 20:12 
GeneralRe: My vote of 5 Pin
Brij8-May-11 3:46
mentorBrij8-May-11 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.