Tab to next textbox after max length value is reached. Error in Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spoonybard
    New Member
    • Nov 2007
    • 19

    Tab to next textbox after max length value is reached. Error in Firefox

    Hi Everyone,

    I have an ASP.Net 2.0 C# web application that requires a user to enter in two sets of numbers. The first number is broken into 4 textboxes and the second number is broken into 2 textboxes.

    The first number 4 textboxes have their max length property set to: 3, 4, 4, 3.
    The second number 2 textboxes have their max length property set to: 4, 4.

    The code I have works fine in IE, but does not work in Firefox. I was hoping someone can help point me in the right direction to enable my code to be cross-platform compatible. Thanks in advance and here is my code:

    [CODE=javascript]<script language="javas cript" type="text/javascript">
    ns4 = (document.layer s)? true:false;
    ie4 = (document.all)? true:false;
    if (ns4)
    document.captur eEvents(Event.K EYUP);

    document.onkeyu p = checkLength;

    function checkLength(e)
    {
    if (ns4)
    {
    var key = e.which
    var el = e.target;
    var str = String(e.target );
    str = str.toLowerCase ();
    if (str.indexOf('i nput') != -1)
    el.tagName = "INPUT"
    else
    return;

    str = str.slice(str.i ndexOf('maxleng th'));
    str = str.slice(str.i ndexOf('=') + 1);

    if ((!isNaN(parseI nt(str))) && (parseInt(str) != -1))
    el.maxLength = parseInt(str);
    else
    el.maxLength = 2147483647;
    }
    else
    {
    var key = event.keyCode;
    var el = event.srcElemen t;
    }

    if (el.tagName == "INPUT" && key != 8)
    {
    if (el.value.lengt h + 1 > el.maxLength)
    {
    var i;
    for(i = 0; i < el.form.element s.length; i++)
    {
    if (el == el.form.element s[i])
    break;
    }

    //if there is one, put the focus on the next element
    if (i != el.form.element s.length - 1)
    {
    if (el.form.elemen ts[i + 1].type != "hidden")
    el.form.element s[i + 1].focus();
    }
    else
    {
    for(i = 0; i < el.form.element s.length; i++)
    {
    if (el.form.elemen ts[i].type != "hidden")
    {
    el.form.element s[i].focus();
    break;
    }
    }
    }
    }
    }
    }
    </script>[/CODE]
    Last edited by acoder; Nov 28 '07, 07:11 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    Argh! This is very old IE4/NS4 browser branching code.

    Instead of using browser detection, test for the object, e.g. if (e) { ...

    See this example.

    Comment

    • spoonybard
      New Member
      • Nov 2007
      • 19

      #3
      The example you provided worked perfectly. Thank you so much for you help!

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No problem, glad it helped. Post again if you have more questions.

        Comment

        Working...