+ Reply to Thread
Results 1 to 8 of 8

Thread: Auto tab movement

  1. #1
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    Auto tab movement

    Hey guys
    Sorry for the awkward title.

    I have a .aspx page on which i have 3 text boxes for phone number
    1st and 2nd text boxes have length 3 and the third one has length 4.

    Now my client wants that when he enters 3 numbers in 1st text box, he dont need to press "tab" and the cursor automatically goes to the 2nd text box and then to the 3rd textbox.

    Is that possible?

    Thanx

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    That is possible, yes...

    If you add an onkeyup event handler to the textbox to check. Something like:-
    Code:
    function checkSize(chkElem, nxtElem, size)
    {
    var elem = document.getElementById(chkElem);
    if(elem.length==size) document.getElementById(nxtElem).focus();
    }
    
    Add it to your textboxes in your code behind like so:-
    Code:
    this.txtBox1.Attributes.Add("onkeyup","checkSize('" + this.txtBox1.ClientId + "','" + this.txtBox2.ClientId + "',3)");
    
    Off the top of my head, so can't guarantee it'll work straight out of the box...

    Hope that helps.

  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Actually, I think the JS should be this:-
    Code:
    function checkSize(chkElem, nxtElem, size)
    {
    var elem = document.getElementById(chkElem);
    if(elem.value.length==size) document.getElementById(nxtElem).focus();
    }
     


  4. #4
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by richyrich View Post
    Actually, I think the JS should be this:-
    Code:
    function checkSize(chkElem, nxtElem, size)
    {
    var elem = document.getElementById(chkElem);
    if(elem.value.length==size) document.getElementById(nxtElem).focus();
    }
     

    thanx RR............. this works

  5. #5
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    790
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    Well, this is the simple case. If you want more features like digits only or having backspace jump to "previous" textbox, add this JS code to the head:
    Code:
    <script type="text/javascript">
    var ASCII_CODE_0 = 48;
    var ASCII_CODE_9 = 57;
    var ASCII_CODE_BACKSPACE = 8;
    var ASCII_CODE_ARROW_LEFT = 37;
    var ASCII_CODE_ARROW_RIGHT = 39;
    var ASCII_CODE_ARROW_UP = 38;
    var ASCII_CODE_ARROW_DOWN = 40;
    
    //Set focus on target element when given input value length is equal or more than max chars.
    function AutoJump(evt, oInput)
    {
    	if (typeof evt == "undefined" || !evt)
    		evt = window.event;
    
    	var code = (evt.keyCode || evt.charCode);
    
    	//ignore arrow keys
    	if (code == ASCII_CODE_ARROW_LEFT || code == ASCII_CODE_ARROW_RIGHT || code == ASCII_CODE_ARROW_UP || code == ASCII_CODE_ARROW_DOWN)
    		return false;
    	
    	//read required attributes:
    	var nMaxChars = parseInt(ReadAutoJumpAttribute(oInput, "aj_maxchar"));
    	var sTargetID = ReadAutoJumpAttribute(oInput, "aj_target");
    	if (isNaN(nMaxChars) || sTargetID.length == 0)
    		return false;
    
    	if (oInput.value.length >= nMaxChars)
    	{
    		var oTarget = document.getElementById(sTargetID);
    		if (!oTarget)
    		{
    			alert("target element " + sTargetID + " does not exist");
    			return false;
    		}
    		oTarget.focus();
    	}
    	
    	//jump back? (backspace in empty box)
    	if (evt.keyCode == ASCII_CODE_BACKSPACE && oInput.value.length == 0 && oInput.getAttribute("aj_isempty") == "true")
    	{
    		//search for input having this one as target
    		var arrInputs = document.getElementsByTagName("input");
    		for (var i = 0; i < arrInputs.length; i++)
    		{
    			var oCurInput = arrInputs[i];
    			var sCurTarget = oCurInput.getAttribute("aj_target");
    			if (sCurTarget && sCurTarget == oInput.id)
    			{
    				oCurInput.focus();
    				var nLength = oCurInput.value.length;
    				if (nLength > 0)
    					doSetCaretPosition(oCurInput, nLength + 1);
    				break;
    			}
    		}
    	}
    	
    	return true;
    }
    
    function DigitsOnly(evt)
    {
    	if (typeof evt == "undefined" || !evt)
    		evt = window.event;
    	
    	var code = (evt.keyCode || evt.charCode);
    	
    	return (code >= ASCII_CODE_0 && code <= ASCII_CODE_9);
    }
    
    function CheckIsEmpty(oInput)
    {
    	oInput.setAttribute("aj_isempty", oInput.value.length == 0 ? "true" : "false");
    }
    
    function ReadAutoJumpAttribute(element, attName)
    {
    	var attValue = element.getAttribute(attName);
    	if (!attValue || attValue.length == 0)
    		alert("missing attribute in " + element.id + ": " + attName);
    	return attValue;
    }
    </script>
    
    <script language="JavaScript" type="text/javascript">
    
       /*
       **  Returns the caret (cursor) position of the specified text field.
       **  Return value range is 0-oField.length.
       */
       function doGetCaretPosition (oField) {
    
         // Initialize
         var iCaretPos = 0;
    
         // IE Support
         if (document.selection) { 
    
           // Set focus on the element
           oField.focus ();
      
           // To get cursor position, get empty selection range
           var oSel = document.selection.createRange ();
      
           // Move selection start to 0 position
           oSel.moveStart ('character', -oField.value.length);
      
           // The caret position is selection length
           iCaretPos = oSel.text.length;
         }
    
         // Firefox support
         else if (oField.selectionStart || oField.selectionStart == '0')
           iCaretPos = oField.selectionStart;
    
         // Return results
         return (iCaretPos);
       }
    
    
       /*
       **  Sets the caret (cursor) position of the specified text field.
       **  Valid positions are 0-oField.length.
       */
       function doSetCaretPosition (oField, iCaretPos) {
    
         // IE Support
         if (document.selection) { 
    
           // Set focus on the element
           oField.focus ();
      
           // Create empty selection range
           var oSel = document.selection.createRange ();
      
           // Move selection start and end to 0 position
           oSel.moveStart ('character', -oField.value.length);
      
           // Move selection start and end to desired position
           oSel.moveStart ('character', iCaretPos);
           oSel.moveEnd ('character', 0);
           oSel.select ();
         }
    
         // Firefox support
         else if (oField.selectionStart || oField.selectionStart == '0') {
           oField.selectionStart = iCaretPos;
           oField.selectionEnd = iCaretPos;
           oField.focus ();
         }
       }
    
    </script>
    
    Then have this in your HTML:
    Code:
    <input type="text" id="phone1" maxlength="3" size="3" aj_maxchar="3" aj_target="phone2" onkeyup="AutoJump(event, this);" onkeypress="return DigitsOnly(event);" onkeydown="CheckIsEmpty(this);" /> - 
    <input type="text" id="phone2" maxlength="3" size="3" aj_maxchar="3" aj_target="phone3" onkeyup="AutoJump(event, this);" onkeypress="return DigitsOnly(event);" onkeydown="CheckIsEmpty(this);" /> - 
    <input type="text" id="phone3" maxlength="4" size="4" aj_maxchar="4" aj_target="phone1" onkeyup="AutoJump(event, this);" onkeypress="return DigitsOnly(event);" onkeydown="CheckIsEmpty(this);" />
    
    Tested on IE8 and Chrome, not sure if it's 100% working in other browsers.

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Wow...That's pretty comprehensive SW....Nice

  7. #7
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,758
    Blog Entries
    2
    Rep Power
    8

    thats incredible Shad.......... but it also gave me brain fart

  8. #8
    Ask Me About Dragons :D Shadow Wizard has a spectacular aura about Shadow Wizard has a spectacular aura about Shadow Wizard's Avatar
    Join Date
    Jul 2008
    Location
    Israel
    Posts
    790
    Blog Entries
    2
    Real Name
    Yahav
    Rep Power
    5

    lol M.. you don't have to understand it right away, start by using it and try understanding slowly but surely.

+ Reply to Thread

Similar Threads

  1. Crystal Reports - Auto Run
    By Flam in forum Crystal Reports Help
    Replies: 2
    Last Post: November 11th, 2009, 02:01 AM
  2. VS 2008 Annoying Auto Format
    By dr_rock in forum .NET Development
    Replies: 2
    Last Post: March 10th, 2009, 10:06 PM
  3. Finding Datalist Row on auto postback
    By richyrich in forum .NET Development
    Replies: 1
    Last Post: May 12th, 2008, 05:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO