Setting Focus on field when RequiredFieldValidator fails - This Works!

S

Steve Yerkes

There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There are a couple of people selling components... but that is not really an option for me... So, I took the plunge and modified the "WebUIValidation.js" file to make it happen... After tracing through file, I figure it out. It was actually pretty easy... I am really surprised that Microsoft did not include this functionality by default... anyway, here is the solution and code for you all to tweak.. :)

1.. Create an ASP.NET web application in C#
2.. Create some form elements
3.. Drag a RequiredFieldValidator next to one of your fields
4.. Build and browse
5.. In your browser, view the html source code.
6.. Search for a string in the source code called "WebUIValidation.js" and take a note of the full path. You will need to open this file in the next step. For example, my full path was...

C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js

7.. So, go ahead and open the file from step 6: "WebUIValidation.js" *** Make a backup first (just in case!)
8.. We are only going to modify the "ValidatorUpdateIsValid()" function. Modify the function to look like the following (I have included the entire function here... so you can actually just overwrite yours with this one):


function ValidatorUpdateIsValid() {

var i;

for (i = 0; i < Page_Validators.length; i++) {


if (!Page_Validators.isvalid) {

Page_IsValid = false;


//---[ YERKES * 06/29/03 ]--------------------------------------

// The code in this function was added to allow field

// focusing when a validator is not valid.

//

// Uncomment the following 'alert' if you want a pop-up

// to display the error message along with it being dispalyed

// on the form.

//--------------------------------------------------------------

//alert(Page_Validators.errormessage);


var strControlToValidate = Page_Validators.controltovalidate;

var strControlType = document.forms[0].elements[strControlToValidate].type;

if (strControlType != "text"){

document.forms[0].elements[strControlToValidate][0].focus();

return


} else {

document.forms[0].elements[strControlToValidate].focus();

return;

}

}

}

Page_IsValid = true;

}


9.. Save this File and "refesh" your aspx page in the browser. The changes should have taken effect.


That's it... this works for text fields and radiobuttonlists... that is all I have tested but it should set focus to any field if it fails any validation for any reason (and not submt the form, of course).

I have also attached my WebUIValidation.js just so you can have the whole thing.. but like I said, we only added a couple lines of code to 1 function... not bad. :)

Peace out and remember.. J2EE is for punks..
 
Y

Yan-Hong Huang[MSFT]

Hello Steve,

Thanks very much for the post in the group. I will look into it and file
this into our knowlege base database. We are looking at continual
improvement in VS.NET, and it's this kind of feedback that let's us know
what things you're trying to do, that we haven't yet exposed for you.

Thanks again and hope you enjoy the group.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
--------------------
!From: "Steve Yerkes" <[email protected]>
!Subject: Setting Focus on field when RequiredFieldValidator fails - This
Works!
!Date: Sun, 29 Jun 2003 04:24:36 -0700
!Lines: 759
!MIME-Version: 1.0
!Content-Type: multipart/mixed;
! boundary="----=_NextPart_000_000B_01C33DF6.5957C7C0"
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: adsl-63-204-75-83.dsl.scrm01.pacbell.net 63.204.75.83
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155675
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!There seems to be way too much confusion over how to set focus on the a
field using a field validator. I looked all over the web and found people
trying to do this, but not getting anywhere. There are a couple of people
selling components... but that is not really an option for me... So, I
took the plunge and modified the "WebUIValidation.js" file to make it
happen... After tracing through file, I figure it out. It was actually
pretty easy... I am really surprised that Microsoft did not include this
functionality by default... anyway, here is the solution and code for you
all to tweak.. :)
! 1.. Create an ASP.NET web application in C#
! 2.. Create some form elements
! 3.. Drag a RequiredFieldValidator next to one of your fields
! 4.. Build and browse
! 5.. In your browser, view the html source code.
! 6.. Search for a string in the source code called "WebUIValidation.js"
and take a note of the full path. You will need to open this file in the
next step. For example, my full path was...
!
C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
! 7.. So, go ahead and open the file from step 6: "WebUIValidation.js" ***
Make a backup first (just in case!)
! 8.. We are only going to modify the "ValidatorUpdateIsValid()" function.
Modify the function to look like the following (I have included the entire
function here... so you can actually just overwrite yours with this one):
! function ValidatorUpdateIsValid() {
! var i;
! for (i = 0; i < Page_Validators.length; i++) {
! if (!Page_Validators.isvalid) {
! Page_IsValid = false;
! //---[ YERKES * 06/29/03 ]--------------------------------------
! // The code in this function was added to allow field
! // focusing when a validator is not valid.
! //
! // Uncomment the following 'alert' if you want a pop-up
! // to display the error message along with it being dispalyed
! // on the form.
! //--------------------------------------------------------------
! //alert(Page_Validators.errormessage);
! var strControlToValidate = Page_Validators.controltovalidate;
! var strControlType =
document.forms[0].elements[strControlToValidate].type;
! if (strControlType != "text"){
! document.forms[0].elements[strControlToValidate][0].focus();
! return
! } else {
! document.forms[0].elements[strControlToValidate].focus();
! return;
! }
! }
! }
! Page_IsValid = true;
! }
! 9.. Save this File and "refesh" your aspx page in the browser. The
changes should have taken effect.
!That's it... this works for text fields and radiobuttonlists... that is
all I have tested but it should set focus to any field if it fails any
validation for any reason (and not submt the form, of course).
!I have also attached my WebUIValidation.js just so you can have the whole
thing.. but like I said, we only added a couple lines of code to 1
function... not bad. :)
!Peace out and remember.. J2EE is for punks..
!
 
A

Alan

Hi,
I tried this and receive an error message from a part of the code that
i'm not messing with at all....

Hello Steve,

Thanks very much for the post in the group. I will look into it and file
this into our knowlege base database. We are looking at continual
improvement in VS.NET, and it's this kind of feedback that let's us know
what things you're trying to do, that we haven't yet exposed for you.

Thanks again and hope you enjoy the group.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
--------------------
!From: "Steve Yerkes" <[email protected]>
!Subject: Setting Focus on field when RequiredFieldValidator fails - This
Works!
!Date: Sun, 29 Jun 2003 04:24:36 -0700
!Lines: 759
!MIME-Version: 1.0
!Content-Type: multipart/mixed;
! boundary="----=_NextPart_000_000B_01C33DF6.5957C7C0"
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!NNTP-Posting-Host: adsl-63-204-75-83.dsl.scrm01.pacbell.net 63.204.75.83
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155675
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!There seems to be way too much confusion over how to set focus on the a
field using a field validator. I looked all over the web and found people
trying to do this, but not getting anywhere. There are a couple of people
selling components... but that is not really an option for me... So, I
took the plunge and modified the "WebUIValidation.js" file to make it
happen... After tracing through file, I figure it out. It was actually
pretty easy... I am really surprised that Microsoft did not include this
functionality by default... anyway, here is the solution and code for you
all to tweak.. :)
! 1.. Create an ASP.NET web application in C#
! 2.. Create some form elements
! 3.. Drag a RequiredFieldValidator next to one of your fields
! 4.. Build and browse
! 5.. In your browser, view the html source code.
! 6.. Search for a string in the source code called "WebUIValidation.js"
and take a note of the full path. You will need to open this file in the
next step. For example, my full path was...
!
C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
! 7.. So, go ahead and open the file from step 6: "WebUIValidation.js" ***
Make a backup first (just in case!)
! 8.. We are only going to modify the "ValidatorUpdateIsValid()" function.
Modify the function to look like the following (I have included the entire
function here... so you can actually just overwrite yours with this one):
! function ValidatorUpdateIsValid() {
! var i;
! for (i = 0; i < Page_Validators.length; i++) {
! if (!Page_Validators.isvalid) {
! Page_IsValid = false;
! //---[ YERKES * 06/29/03 ]--------------------------------------
! // The code in this function was added to allow field
! // focusing when a validator is not valid.
! //
! // Uncomment the following 'alert' if you want a pop-up
! // to display the error message along with it being dispalyed
! // on the form.
! //--------------------------------------------------------------
! //alert(Page_Validators.errormessage);
! var strControlToValidate = Page_Validators.controltovalidate;
! var strControlType =
document.forms[0].elements[strControlToValidate].type;
! if (strControlType != "text"){
! document.forms[0].elements[strControlToValidate][0].focus();
! return
! } else {
! document.forms[0].elements[strControlToValidate].focus();
! return;
! }
! }
! }
! Page_IsValid = true;
! }
! 9.. Save this File and "refesh" your aspx page in the browser. The
changes should have taken effect.
!That's it... this works for text fields and radiobuttonlists... that is
all I have tested but it should set focus to any field if it fails any
validation for any reason (and not submt the form, of course).
!I have also attached my WebUIValidation.js just so you can have the whole
thing.. but like I said, we only added a couple lines of code to 1
function... not bad. :)
!Peace out and remember.. J2EE is for punks..
!
 
Y

Yan-Hong Huang[MSFT]

Hello Alan,

I think you could contact Steve in the group or email to see if he could provide more info. I suggest you provide detailed error
message and the info that where the error happened. Thanks.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: (e-mail address removed) (Alan)
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Subject: Re: Setting Focus on field when RequiredFieldValidator fails - This Works!
!Date: 31 Jul 2003 11:50:51 -0700
!Organization: http://groups.google.com/
!Lines: 103
!Message-ID: <[email protected]>
!References: <[email protected]> <[email protected]>
!NNTP-Posting-Host: 12.150.117.131
!Content-Type: text/plain; charset=ISO-8859-1
!Content-Transfer-Encoding: 8bit
!X-Trace: posting.google.com 1059677452 4261 127.0.0.1 (31 Jul 2003 18:50:52 GMT)
!X-Complaints-To: (e-mail address removed)
!NNTP-Posting-Date: 31 Jul 2003 18:50:52 GMT
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!
news.maxwell.syr.edu!sn-xit-03!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:163885
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Hi,
!I tried this and receive an error message from a part of the code that
!i'm not messing with at all....
!
[email protected] (Yan-Hong Huang[MSFT]) wrote in message @cpmsftngxa09.phx.gbl>...
!> Hello Steve,
!>
!> Thanks very much for the post in the group. I will look into it and file
!> this into our knowlege base database. We are looking at continual
!> improvement in VS.NET, and it's this kind of feedback that let's us know
!> what things you're trying to do, that we haven't yet exposed for you.
!>
!> Thanks again and hope you enjoy the group.
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no rights.
!> Got .Net? http://www.gotdotnet.com
!> --------------------
!> !From: "Steve Yerkes" <[email protected]>
!> !Subject: Setting Focus on field when RequiredFieldValidator fails - This
!> Works!
!> !Date: Sun, 29 Jun 2003 04:24:36 -0700
!> !Lines: 759
!> !MIME-Version: 1.0
!> !Content-Type: multipart/mixed;
!> ! boundary="----=_NextPart_000_000B_01C33DF6.5957C7C0"
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
!> !Message-ID: <[email protected]>
!> !Newsgroups: microsoft.public.dotnet.framework.aspnet
!> !NNTP-Posting-Host: adsl-63-204-75-83.dsl.scrm01.pacbell.net 63.204.75.83
!> !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
!> !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155675
!> !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!> !
!> !There seems to be way too much confusion over how to set focus on the a
!> field using a field validator. I looked all over the web and found people
!> trying to do this, but not getting anywhere. There are a couple of people
!> selling components... but that is not really an option for me... So, I
!> took the plunge and modified the "WebUIValidation.js" file to make it
!> happen... After tracing through file, I figure it out. It was actually
!> pretty easy... I am really surprised that Microsoft did not include this
!> functionality by default... anyway, here is the solution and code for you
!> all to tweak.. :)
!> ! 1.. Create an ASP.NET web application in C#
!> ! 2.. Create some form elements
!> ! 3.. Drag a RequiredFieldValidator next to one of your fields
!> ! 4.. Build and browse
!> ! 5.. In your browser, view the html source code.
!> ! 6.. Search for a string in the source code called "WebUIValidation.js"
!> and take a note of the full path. You will need to open this file in the
!> next step. For example, my full path was...
!> !
!> C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
!> ! 7.. So, go ahead and open the file from step 6: "WebUIValidation.js" ***
!> Make a backup first (just in case!)
!> ! 8.. We are only going to modify the "ValidatorUpdateIsValid()" function.
!> Modify the function to look like the following (I have included the entire
!> function here... so you can actually just overwrite yours with this one):
!> ! function ValidatorUpdateIsValid() {
!> ! var i;
!> ! for (i = 0; i < Page_Validators.length; i++) {
!> ! if (!Page_Validators.isvalid) {
!> ! Page_IsValid = false;
!> ! //---[ YERKES * 06/29/03 ]--------------------------------------
!> ! // The code in this function was added to allow field
!> ! // focusing when a validator is not valid.
!> ! //
!> ! // Uncomment the following 'alert' if you want a pop-up
!> ! // to display the error message along with it being dispalyed
!> ! // on the form.
!> ! //--------------------------------------------------------------
!> ! //alert(Page_Validators.errormessage);
!> ! var strControlToValidate = Page_Validators.controltovalidate;
!> ! var strControlType =
!> document.forms[0].elements[strControlToValidate].type;
!> ! if (strControlType != "text"){
!> ! document.forms[0].elements[strControlToValidate][0].focus();
!> ! return
!> ! } else {
!> ! document.forms[0].elements[strControlToValidate].focus();
!> ! return;
!> ! }
!> ! }
!> ! }
!> ! Page_IsValid = true;
!> ! }
!> ! 9.. Save this File and "refesh" your aspx page in the browser. The
!> changes should have taken effect.
!> !That's it... this works for text fields and radiobuttonlists... that is
!> all I have tested but it should set focus to any field if it fails any
!> validation for any reason (and not submt the form, of course).
!> !I have also attached my WebUIValidation.js just so you can have the whole
!> thing.. but like I said, we only added a couple lines of code to 1
!> function... not bad. :)
!> !Peace out and remember.. J2EE is for punks..
!> !
!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top