5,693,062 members and growing! (18,928 online)
Email Password   helpLost your password?
Web Development » Validation » General     Intermediate

Transferring Control Focus through Custom Validation File, in ASP.NET

By bkalicharan

Validation controls will not set the focus to the appropriate control when the validation fails. This can be achieved using a custom validation file.
Javascript, XML, Windows, .NET, Visual Studio, WebForms, ASP.NET, Dev

Posted: 12 May 2005
Updated: 12 May 2005
Views: 14,681
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.12 Rating: 2.73 out of 5
3 votes, 50.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 16.7%
4
2 votes, 33.3%
5

Introduction

In one of my recent projects, I faced the problem of how to transfer control focus to the appropriate control when validation fails. I was confused with the client requirement at first but realized it is a common and basic requirement from the user point. But it is not implemented in the WebUIValidation.js. You can implement this with a minimal setting change in the web.config file and a little code change in the WebUIValidation file.

Add the following element in the system.web section of the web.config file for using a custom validation file:

<webControls clientScriptsLocation="<virtual_path>/javascripts/"></webControls>

Add a new function in the WebUIValidation which will set the focus to the control which fails in the validation process.

function ValidatorControlFocus()
{
    if(typeof(Page_Validators) != "undefined")
    {
        for (i = 0; i < Page_Validators.length; i++)
        {
            val = Page_Validators[i];
            if(val.isvalid == false)
            {
                control = document.all[val.controltovalidate];
                if(control != null)
                {
                    if( !control.isDisabled ){
                        if(control.style.visibility != "hidden"){
                            control.focus();
                        }
                    }
                    return;
                }
            }
        }
    }
}

Call the above function in Page_ClientValidate as well as in ValidatorCommonOnSubmit functions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

bkalicharan



Occupation: Web Developer
Location: United States United States

Other popular Validation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralWorks fine to move the focus, butmembertotakoori14:24 8 Apr '08  
GeneralTried your solution but web config file causing error when building in debug modememberScoochie11:55 9 Jul '07  
GeneralRe: Tried your solution but web config file causing error when building in debug modememberbkalicharan9:22 10 Jul '07  
GeneralRe: Tried your solution but web config file causing error when building in debug modememberScoochie12:49 10 Jul '07  
GeneralRe: Tried your solution but web config file causing error when building in debug modememberScoochie13:18 10 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 May 2005
Editor: Smitha Vijayan
Copyright 2005 by bkalicharan
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project