posts - 4, comments - 7, trackbacks - 0

My Links

News

Archives

Post Categories

Simple Auto-Tab with jQuery

I think that some well planned auto-tabbing on a web form can really make a page feel polished.


Suppose we have some lovely aspx markup like this:

<form id="aspnet_form" runat="server">
    <label>Social Security Number:</label>
    <asp:TextBox ID="PartOne" MaxLength="3" runat="server" />-
    <asp:TextBox ID="PartTwo" MaxLength="2" runat="server" />-
    <asp:TextBox ID="PartThree" MaxLength="4" runat="server" />
</form>

It would really be nice if the user didn't have to hit tab between these obviously related textboxes.  The JavaScript snippet below can be used to easily enable auto-tabbing in your webforms.


Usage:

<script type="text/javascript">
    $(document).ready(function() {
        WireAutoTab('<%= PartOne.ClientID %>',
            '<%= PartTwo.ClientID %>', 3);
        WireAutoTab('<%= PartTwo.ClientID %>',
            '<%= PartThree.ClientID %>', 2);
    });
</script>

The code:

///<summary>
/// Wires an element's OnKeyUp event to move focus to the next element
/// once a certain number of chacaters have been entered.
///</summary>
///<param name="CurrentElementID">The client ID of the element to tab
/// from.</param>
///<param name="NextElementID">The client ID of the element to tab
/// to.</param>
///<param name="FieldLength">The number of characters that should be
/// allowed before tabbing away from the field.</param>
function WireAutoTab(CurrentElementID, NextElementID, FieldLength) {
    //Get a reference to the two elements in the tab sequence.
    var CurrentElement = $('#' + CurrentElementID);
    var NextElement = $('#' + NextElementID);
 
    CurrentElement.keyup(function(e) {
        //Retrieve which key was pressed.
        var KeyID = (window.event) ? event.keyCode : e.keyCode;
 
        //If the user has filled the textbox to the given length and
        //the user just pressed a number or letter, then move the
        //cursor to the next element in the tab sequence.   
        if (CurrentElement.val().length >= FieldLength
            && ((KeyID >= 48 && KeyID <= 90) ||
            (KeyID >= 96 && KeyID <= 105)))
            NextElement.focus();
    });
}
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Monday, January 26, 2009 2:59 PM | Filed Under [ ASP.NET JavaScript ]

Feedback

Gravatar

# re: Simple Auto-Tab with jQuery

I realize that you posted this like 10 months ago, but nice work!

This is a great solution for purists like me who don't want to mess with jQuery plugins to make forms a bit more user friendly.

Thanks for this!
11/20/2009 12:31 PM | Chris
Gravatar

# re: Simple Auto-Tab with jQuery

Well Thanks a lot for this, Like Chris said you posted it long time ago but it still awesome because it is Simple and Effective !!

Exactly what i was looking for.

Thanks a lot for taking time to share
2/2/2010 2:55 PM | BeNNeS
Gravatar

# re: Simple Auto-Tab with jQuery

I didn't read the first article, because I haven't bumped into it, so I think this is great, it's much easier this way. It's curious how I didn't think about this, I must have had a car cover over me and I couldn't see the solution.
11/25/2010 12:42 PM | Josiah
Gravatar

# re: Simple Auto-Tab with jQuery

Hi
Thanks for the post
I hope you don't mind, but I've update the function a little to be a jquery extension with a tiny amount of additional features

$.fn.autoTab = function (NextElementID, FieldLength) {

//Get a reference to the two elements in the tab sequence.
var CurrentElement = $(this);
var NextElement = $('#' + NextElementID);

CurrentElement.focus(function () {
$(this).select();
});

CurrentElement.keyup(function (e) {
//Retrieve which key was pressed.
var KeyID = (window.event) ? event.keyCode : e.keyCode;
var thisElement = $(this);

//If the user has filled the textbox to the given length and
//the user just pressed a number or letter, then move the
//cursor to the next element in the tab sequence.
if (thisElement.val().length >= FieldLength && ((KeyID >= 48 && KeyID <= 90) || (KeyID >= 96 && KeyID <= 105))) {

var currentValue = thisElement.val();
thisElement.val(currentValue.substring(0, FieldLength));
NextElement.focus();
NextElement.select();
}
});

return this;
}
12/9/2010 12:54 PM | hendrik
Gravatar

# re: Simple Auto-Tab with jQuery

Works great!
4/11/2011 2:09 AM | Steve
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: