Control Tab Movement in CustomValidation Script

Discussion in 'ASP .Net' started by kpg, Dec 21, 2004.

  1. kpg

    kpg Guest

    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
     
    kpg, Dec 21, 2004
    #1
    1. Advertising

  2. kpg

    kpg Guest

    <%@ 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>
     
    kpg, Dec 21, 2004
    #2
    1. Advertising

  3. kpg

    Peter Blum Guest

    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:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "kpg" <> wrote in message
    news:...
    >
    > <%@ 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>
    >
     
    Peter Blum, Dec 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cm9kY2hhcg==?=

    datagrid template columns and customvalidation

    =?Utf-8?B?cm9kY2hhcg==?=, Oct 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    433
    =?Utf-8?B?RmVybmFuZG8gSHVudGg=?=
    Jan 11, 2005
  2. =?Utf-8?B?Ym9iYnk=?=

    CustomValidation Control

    =?Utf-8?B?Ym9iYnk=?=, Sep 12, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    325
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
    Sep 13, 2007
  3. Craig Buchanan
    Replies:
    1
    Views:
    370
    Peter Bucher [MVP]
    Sep 10, 2008
  4. Craig Buchanan

    Web User Control with CustomValidation and Javascript

    Craig Buchanan, Aug 18, 2008, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    775
    Peter Bucher [MVP]
    Sep 10, 2008
  5. Craig Buchanan

    Web User Control with CustomValidation and Javascript

    Craig Buchanan, Aug 18, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    154
    Peter Bucher [MVP]
    Sep 10, 2008
Loading...

Share This Page