A few thoughts.
1. The customvalidator function will run when the user clicks Submit too. So
expect that alert and focus to be set if the user clicks submit. That may
break your UI goals.
2. I rewrote Microsoft's validators with "Professional Validation And More"
(
http://www.peterblum.com/vam/home.aspx). It includes options to set focus
to the textbox and popup an alert. It has separate rules for onchange vs
onsubmit actions. Professional Validation And More is loaded with
improvements over Microsoft's validators designed to avoid all the custom
coding and hacks you are facing now. You may not even need to write a
customvalidator due to the extensive validators I include.
3. In your code, the call document.getElementById("Textbox1").focus(); looks
fine. However, in my experience, I have found that it doesn't work well when
back-to-back with an alert box. That most likely is the problem here. I
solved this by using the window.setTimeout() function to set focus after a
short delay.
--- Peter Blum
www.PeterBlum.com Email: PLBlum@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx [quoted text, click to view] "Karen Grube" <KarenGrube@discussions.microsoft.com> wrote in message
news:2EDE6180-DBDE-4BC4-8012-14962A5AA11D@microsoft.com...
> Hi!
>
> I have a control on a web page and a custom validator associated with
> that
> control, along with a client side javascript validaton function. The
> validation is working well with one exception. I'm trying to keep focus
> on
> that control until the user enters something valid, but I'm having trouble
> doing that.
>
> The routine is setting Args.IsValid correctly, and I even had "return
> Args.IsValid" as the last line of the routine thinking that if it was
> false
> it would return the user to the control. Then I added the last line of
> code
> right before the end of the function, and it still isn't returning focus
> to
> the control, assuming the controlid is "Textbox1":
>
> function testinput (Source, Args)
> (
> Args.IsValid = false;
> if (Args.Value == "Test")
> Args.IsValid = true;
>
> if (Args.IsValid == false)
> {
> alert("Invalid entry. Please try again.");
> document.getElementById("Textbox1").focus();
> }
> }
>
> I must have something wrong. What do I need to do to return focus to a
> control until the user inputs something correct? Oh, and yes I did try
> using
> the document.all and form syntax as well, with no luck.
>
> Am I wrong in thinking that if I set focus in a cusstom validator's
> client side routine that it should work? Is there something about the
> alert
> box that messes it up?
>
> Please let me know.
>
> Thanks!
> Kaen
>