473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">     
  2.         ns4 = (document.layers)? true:false;
  3.         ie4 = (document.all)? true:false;
  4.         if (ns4)
  5.            document.captureEvents(Event.KEYUP);
  6.  
  7.         document.onkeyup = checkLength;
  8.  
  9.         function checkLength(e)
  10.         {
  11.            if (ns4)
  12.            {
  13.               var key = e.which
  14.               var el = e.target;
  15.               var str = String(e.target);
  16.               str = str.toLowerCase();
  17.               if (str.indexOf('input') != -1)
  18.                  el.tagName = "INPUT"
  19.               else
  20.                  return;
  21.  
  22.               str = str.slice(str.indexOf('maxlength'));
  23.               str = str.slice(str.indexOf('=') + 1);
  24.  
  25.               if ((!isNaN(parseInt(str))) && (parseInt(str) != -1)) 
  26.                    el.maxLength = parseInt(str);
  27.               else 
  28.                  el.maxLength = 2147483647;
  29.            }
  30.            else
  31.            {
  32.               var key = event.keyCode;
  33.               var el = event.srcElement;
  34.            }
  35.  
  36.            if (el.tagName == "INPUT" && key != 8)
  37.            {
  38.               if (el.value.length + 1 > el.maxLength) 
  39.               {
  40.                    var i;
  41.                  for(i = 0; i < el.form.elements.length; i++)
  42.                  {
  43.                       if (el == el.form.elements[i])
  44.                            break;
  45.                  }
  46.  
  47.                  //if there is one, put the focus on the next element
  48.                  if (i != el.form.elements.length - 1)
  49.                  {
  50.                       if (el.form.elements[i + 1].type != "hidden") 
  51.                          el.form.elements[i + 1].focus();
  52.                  }
  53.                  else 
  54.                  {
  55.                       for(i = 0; i < el.form.elements.length; i++)
  56.                       {
  57.                          if (el.form.elements[i].type != "hidden") 
  58.                          {
  59.                             el.form.elements[i].focus();
  60.                             break;
  61.                          }
  62.                       }
  63.                  }
  64.               }
  65.            }
  66.         }
  67. </script>
Nov 28 '07 #1
3 2832
acoder
16,027 Expert Mod 8TB
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.
Nov 28 '07 #2
The example you provided worked perfectly. Thank you so much for you help!
Nov 28 '07 #3
acoder
16,027 Expert Mod 8TB
No problem, glad it helped. Post again if you have more questions.
Nov 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gil | last post by:
I am trying to advance to the next textbox when input for the current textbox has reached its maximum length. In a different newsgroup I found this code snipit, but I have not been able to use it....
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
0
by: Waran | last post by:
I need to create a Auto suggests Textboox like in http://www.google.com/webhp?complete=1&hl=en I have completed this using AJAX.NET for Framework 1.1 . I have some design issues after the data is...
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
0
by: sjickells | last post by:
Hi I am having a problem using asp:TextBox's in a transparent table. I have a background image on the page and a table in the middle of the page. I have set the background colour of the table...
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
1
by: netnatter | last post by:
I am trying to allow my users to paste text from a dropdown list into a textbox at the current cursor position I searched and found some code in a forum but it does not seem to work As a...
13
by: Stever1975 | last post by:
I'm working on something similiar to a shopping cart item page. There is a table of items. Each item has an image, a textbox for the qty and an image for the add button. When the add image is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.