Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Control Tab Movement in CustomValidation Script

Reply
Thread Tools

Control Tab Movement in CustomValidation Script

 
 
kpg
Guest
Posts: n/a
 
      12-21-2004
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



 
Reply With Quote
 
 
 
 
kpg
Guest
Posts: n/a
 
      12-21-2004

<%@ 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>

 
Reply With Quote
 
 
 
 
Peter Blum
Guest
Posts: n/a
 
      12-22-2004
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"kpg" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> <%@ 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>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web User Control with CustomValidation and Javascript Craig Buchanan ASP .Net 1 09-10-2008 11:29 AM
Web User Control with CustomValidation and Javascript Craig Buchanan ASP .Net Web Controls 1 09-10-2008 11:29 AM
Web User Control with CustomValidation and Javascript Craig Buchanan ASP .Net Building Controls 1 09-10-2008 11:29 AM
CustomValidation Control =?Utf-8?B?Ym9iYnk=?= ASP .Net 1 09-13-2007 07:14 AM
datagrid template columns and customvalidation =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 2 01-11-2005 04:29 PM



Advertisments