Control Tab Movement in CustomValidation Script

K

kpg

Hi all,

This should be simple...

I have a TextBox1 and a customvalidation control linked to it.
I use a client side script to validate the textbox.
If the data is not valid I want to return focus to the textbox.
I tried document.GetElementByID("TextBox1").focus(); and
doucment.Form1.TextBox1.focus(); to no avail.

How can I return focus to the TextBox1 after the uses hits
<TAB> to move and the client side script detects an error?

Thanks.
kpg
 
K

kpg

<%@ Page Language="VB" AutoEventWireup="True" %>
<HTML>
<body>
<form id="Form1" runat="server">
<h3><asp:textbox id="Text1" runat="server"></asp:textbox>&nbsp;&nbsp;
<asp:customvalidator id="CustomValidator1" runat="server"
ControlToValidate="Text1" ErrorMessage="CustomValidator"
ClientValidationFunction="MyTest"></asp:customvalidator></h3>
<p><asp:button id="Button1" runat="server" Text="Validate"></asp:button>
</form>
<script language="vbscript">
sub MyTest(source, arguments)
arguments.IsValid=false
document.Form1.Text1.focus
End Sub
</script>
</P>
</body>
</HTML>
 
P

Peter Blum

A few comments:

1. I strongly recommend using Javascript over VBScript. VBScript is
supported on IE and IE/Mac. Other browsers may generate errors when they hit
your script. The code is very easy to convert:
<script language="javascript">
function MyTest(source, arguments)
{
arguments.IsValid=false;
document.Form1.Text1.focus();
}
</script>

2. Be sure to treat everything case sensitively. You wrote "GetElementByID"
when the name is "getElementById()"

3. Be sure that functions have () after them. You wrote focus, not focus().

4. While the idea is pretty good, this custom validator will be fired when
the user edits ControlToEvaluate and on submit. If you use this technique is
several validators, when you click submit, they will all attempt to set
focus and the last one with the error will win.

5. "Professional Validation And More"
(http://www.peterblum.com/vam/home.aspx) is a replacement to Microsoft's
validators that overcomes its numerous limitations so you can avoid custom
coding and hacks. It includes 22 validators that support more browsers than
Microsofts on the client-side.
It has several features designed to draw the user's attention to the field
with the error:
- set focus to the field with separate options for onchange events and on
submit
- change the style of the field
- change the style of the field's label or other nearby element
- show an alert
- blink the error message

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

Forum statistics

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

Latest Threads

Top