JavaScript For Automatically moving Between Textboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    JavaScript For Automatically moving Between Textboxes

    Hi there!

    I'm trying to write a simple JavaScript that will move the cursor to the next TextBox after 5 characters (or numbers) have been entered. I'm attempting to get this working in all browsers and realize that I'll likely have to write more than one script depending on which the user is using.

    Anyways, everything I've tried...I've only come up with something that "sort of" works in IE (only).
    The cursor will be moved to the next TextBox when the count reaches 5 but the 5th character is not printed because it's being replaced by the tab event (keyCode=9).

    This will not work for FireFox, nor Opera. I've changed "keyCode" to be "charCode". ... Alas, it will not tab to the next TextBox...it just prints a space instead.

    So, after attempting this solution I tried to use the focus() method instead.

    I have no idea why, but this won't work in any browser.

    Does anyone have a suggestions on how to go about solving this problem?

    I'm pretty stuck.

    Thanks :)

    -Frinny
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    Why limit yourself to 5?
    This script will advance the focus from any input with a maxLength property when the number of digits or characters equals the maxlength.
    Code:
    <html>
    <head>
    <title>Moving Focus</title>
     
    <script type="text/javascript">
     
    function after5(e){
    	var who= e? e.target: event.srcElement;	
    	if(who.value.length< who.maxLength) return true;	
    	var group= who.form.elements;
     
    	var L= group.length-1, tem;
    	for(var i=0; i<L; i++){
    		tem= group[i];
    		if(tem==who && group[i+1]) return group[++i].focus();
    	}
    }
    onload= function(){
    	var A=document.getElementsByTagName('input');
    	var L= A.length;	
    	for(var i=0; i< L; i++){
    		var tem=A[i];
    		if(tem.maxLength)tem.onkeyup= after5;	
    	}
    	A[0].focus();
    }
    </script>
    </head>
    <body> 
    <h1>Moving focus</h1>
    <form action='' onsubmit="return false;">
    <p><label>First box: 5 characters <input name="in_1" type="text" size="5" maxLength="5" value=""></label></p>
    <p><label>Second box: 5 characters <input name="in_2" type="text" size="5" maxLength="5" value=""></label></p>
    <p><label>Third box: 3 characters <input name="in_3" type="text" size="3" maxLength="3" value=""></label></p>
    <p><label>Fourth box: 7 characters <input name="in_4" type="text" size="7" maxLength="7" value=""></label></p>
    <p><label>Fifth box: 5 characters <input name="in_5" type="text" size="5" maxLength="5" value=""></label></p>
    <p><input type="button" value="Submit"></p>
    </form>
    </body>
    </html>
    Last edited by Frinavale; Sep 22 '10, 03:47 PM. Reason: Fixed code tags

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by mrhoo
      Why limit yourself to 5?
      This script will advance the focus from any input with a maxLength property when the number of digits or characters equals the maxlength.
      ...
      Mr. Hoo you are Awesomel!

      Thank you so much!
      I never even thought about using the max length.
      I ended up finally getting the focus() to change to the next textbox (had a case sensitive issue with my getElementByID( ) )...

      But then ran into a few problems for when the user's input was something like backspace...and even had to write code to check how many characters were in the fields if the focus had changed.

      The max length check gets rid of both these problems completely!
      This is such a complete solution to my problem.
      I hope you don't mind me using it as is.

      Thanks again!
      :)
      -Frinny
      Last edited by Frinavale; Sep 22 '10, 03:47 PM.

      Comment

      Working...