Skip to content

Autotab: jQuery auto-tabbing and filter plugin

Autotab is a jQuery plugin that provides auto-tabbing and filtering on text fields in a form. Once the maximum number of characters has been reached within a defined text fields, the focus is automatically set to the defined target of the element. Likewise, clearing out the text field’s content by pressing backspace eventually places the focus on the elements previous target. NOTE: I still have all of the feedback I have received when my site was running Drupal. When this script moves past 1.1b, I will do my best to incorporate many of the suggestions given.

Why jQuery Autotab and not some other auto-tab script?

  • Auto-tabbing behaves logically, in both tabbing forward and tabbing backwards.
  • It allows you to easily change your text in a tab that would otherwise auto-tab away.
  • It can filter text fields, ie. restrict a phone number fields to just numbers.
  • It’s small, fast, easy to load and built on the powerful jQuery library.

Download

jQuery Autotab Plugin v1.1b (1872692)

Demo

Phone Number:

- -

Social Security Number:

- -

Text characters only:

- -

Alpha only:

- - - -

Uppercase letters and numbers (Converts lowercase letters to uppercase):

- - - -

Any and all characters:

- - - -

Regular Expression (Allows numbers and periods):

Basic Usage

This example provides the benefit that you have less HTML, but the drawback is that you have to modify your selector and specify the maxlength of the field. Six here, half a dozen there; it comes down to preference.

  1. Include jquery.autotab.js in your HTML:
    <script type="text/javascript" src="jquery.autotab.js"></script>
  2. Add your text fields in your HTML, including an ID:
    		<form>
    			<div><strong>Phone Number:</strong></div>
    			<input type="text" name="area_code" id="area_code" maxlength="3" size="3" /> -
    			<input type="text" name="number1" id="number1" maxlength="3" size="3" /> -
    			<input type="text" name="number2" id="number2" maxlength="4" size="5" />
    		</form>
    		
  3. Initialize Autotab on your text fields with a ready() handler:
    		<script type="text/javascript">
    		$(document).ready(function() {
    			$('#area_code, #number1, #number2').autotab_magic().autotab_filter('numeric');
    		});
    		</script>
    		

Advanced Usage

.autotab_magic()

Automatically assign tabbing rules to all selected elements.

Example

		<script type="text/javascript">
		$(document).ready(function() {
			$(':input').autotab_magic();
			// OR
			$('#area_code, #number1, #number2').autotab_magic();
		});
		</script>
		

.autotab_filter(string)

Apply the specified filter to an element.

Example

		<script type="text/javascript">
		$(document).ready(function() {
			$('#area_code, #number1, #number2').autotab_filter('numeric');
			$('#key1, #key2, #key3, #key4, #key5').autotab_filter('alpha');
		});
		</script>
		

Options

  • all (default): Any character
  • text: Any character except numbers 0 through 9
  • alpha: A through Z
  • numeric: 0 through 9
  • number: Identical to numeric
  • alphanumeric: A through Z and 0 through 9
  • custom: A custom regular expression to be used along with the pattern setting.

.autotab_filter(settings)

Apply the key/value pairs to configure filter options to an element.

Example

		<script type="text/javascript">
		$(document).ready(function() {
			$('#area_code, #number1, #number2').autotab_filter({
				format: 'alphanumeric',
				uppercase: true
			});
		});
		</script>
		

Settings

  • format
    The filtering method of the text field. Available filtering options are listed above.
  • uppercase
    Converts any text to uppercase.
  • lowercase
    Converts any text to lowercase.
  • nospace
    Removes any spaces.
  • pattern
    A regular expression pattern to use for custom formats.

.autotab(settings)

Apply the key/value pairs to configure auto-tab and filter options to an element. Filtering settings are optional.

Example

		<script type="text/javascript">
		$(document).ready(function() {
			// This example achieves the same result as Step 3 under Basic Usage.
			$('#area_code').autotab({ target: 'number1', format: 'numeric' });
			$('#number1').autotab({ target: 'number2', format: 'numeric', previous: 'area_code' });
			$('#number2').autotab({ previous: 'number1', format: 'numeric' });
		});
		</script>
		

Settings

  • All listed settings under .autotab_filter()
  • maxlength
    Determines the maximum number of characters allowed in a text field. Passing maxlength will override the hardcoded maxlength attribute in the HTML.
  • target
    The text field to auto-tab to when the maxlength has been reached on the current text field.
  • previous
    The text field to auto-tab to when the Backspace key has been pressed in an empty text field. You can also press and hold Backspace to continue auto-tabbing in reverse.
Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg ThisSubmit to reddit
115 Comments
  1. revive permalink

    Can you provide a clear example using the custom regex option, say for a phone number, social or any other combo.. that uses a single input field?
    Thanks and great work on this plugin!
    J

    • Matthew permalink

      I’ve added an example of a regular expression. It’s a quasi IP filter that I never did get working like I wanted with for separate inputs. Here’s the code:

      $('#regex').autotab_filter({ format: 'custom', pattern: '[^0-9\.]' });
      
      • NRD permalink

        Hi it is great help for me to add autotab plugin in my webbased project.
        I found that when populate the record on the phone numbers then autotab is not work(not moves to another fields there are 2 more txt box)
        Coud help me here asp.
        Thanks

        • Matthew permalink

          I would need to see your example to lend a hand.

  2. nick permalink

    nice script, covered basic validation and autotab in one.

  3. Gregório Ferraz permalink

    Hello friend!
    Great plugin, great!

    Well, I’m using the plugin with jQuery.MaskedInput and found the following behavior:
    When the field with the mask receives the plugin automatically switches the focus to the next field. How can I remove the mask avoiding the focus internally, while displaying the mask to the user?

    Thank you!

    • Matthew permalink

      Not sure, but this is something for me to consider when I rewrite the script.

  4. Arian permalink

    Would be cool to have an option to set an auto-delay tab…
    like when filling out birthday field like 2/7/1982 (so I have three autotabbed fields where each field is set to simple ‘numeric’ validation)…
    if user enters a single digit like 2 and not two numbers, it would be nice if it autotabbed to the day field after a 2 second delay, and same in that day-field.

    (PS I have date validation via validation plugin after user fills out year field).

    • Matthew permalink

      I can see the appeal in that, but that goes against the normal behavior people when typing into limited text fields. It’s bad enough that many people are not well acquainted with auto tabbing on web forms. To introduce a likely and unexpected action is just as bad, if not worse, as other auto tab scripts that make it difficult for you to make a revision to a field because the character limit has been reached.

  5. Josh Withrow permalink

    $(‘StartDate’).autotab({ target: ‘EndDate’, pattern: ‘^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$’ });

    That should format a date string as “yyyy-MM-dd”, but the format isn’t enforced. Any ideas?

    • Matthew permalink

      I’m not great with regular expressions, but I notice your selector lacks a . or #, so it’s looking for an element by the name of StartDate. I’m sure you’ve got this resolved by now though, since you posted this quite a while back.

  6. Is this supported in jQuery 1.4.3?

    • Matthew permalink

      It has not been tested though it may work just fine.

  7. Dave permalink

    Very Good Stuff!.
    Working in JQuery 1.4.3
    3 thumbs up!

  8. hello, I would be very grateful if you could tell me how to focus the element. When the cursor jumps to the next field the cursor blinks but the input box is not focused. Can this be done somehow?

    Thanks in advance

    • Matthew permalink

      Perhaps I’m not understanding you correctly, but if the cursor is in a text box, then the focus is on that text box.

  9. MrMcQ permalink

    Dude…you rock! Thanks for the great plugin!

  10. Alien permalink

    I keep getting an ‘Object required’ javascript error. I’ve linked jquery-1.4.4.js and jquery.autotab-1.1b.js.

    I’ve cut and paste (using the HTML button) the code as needed.

    Any ideas?

    • Alien permalink

      Forget my above comment. It was a small yet stupid mistake on my part. :^)

  11. Anthony W permalink

    Thanks a lot, did exactly what I wanted extremely easily.

  12. Andre permalink

    Thanks for that great plugin!

  13. Camrat permalink

    Awesome plug in….Nice Work! I am doing an inventory system using barcode readers, so users “blib Serial Number” then auto tab, “blip the RID” then auto tab, “blip the last number…. and I would like it to submit the form once the pre determined length was reached. I am going to take a shot at it with your code, but just thought I would post this to see if I might get a response of the correct way to do it.

    Thanks for the work on this.

    • Matthew permalink

      Accomplishing what you’re describing should be possible without modifying AutoTab. Something like this should work:

      $("#lastFormField").change(function() {
          if(this.value.length >= $(this).attr('maxlength'))
              $('#myForm').submit();
      });

      One of the problems I see with this code is manually modifying what is placed into the text field. As soon as you hit your text limit, the form is submitted, even if there’s a typo. You could extend the .change() function to call an outside extension and check a variable; that variable would start as false and be set to true if a keystroke or click is detected for the last text field.

  14. Thanks for the great plugin.

  15. Danny permalink

    When the focus moves from one element to another, the focusout event doesn’t fire on the old element nor does the focusin event fire on the new one. Any idea why?

  16. amazing and very useful plugin.

  17. furcifer permalink

    what about adding a function/method to have the focus to jump to next input programmatically ?

    for example :

    var $autotab = $(“:input”).autotab_magic(…);
    $(“:input:eq(0)”).focus(); // focus is on the first form element of the page
    var second_input_of_the_page = $autotab.autotab(“next”); // focus is on the 2nd form element

  18. aldumal permalink

    im using another plugin but yours is easier to implement
    but how do i put mask on decimal numbers like this?
    the code works if its exacly 3 whole numbers and 2 decimal, not the 2nd example.
    e.g. 123.3, 95.6

    • Matthew permalink

      A tricky issue to solve. Unfortunately Autotab is meant to handled fixed values, so it makes sense that the first, but not the second, would trigger the auto tab. Off the top of my head, I can think of a way to add a new format, such as decimal, and then specifying the number of decimal places to allow before auto tabbing. This would then work for both examples above, provided that the number of decimal places is specified as one. I’ll have to consider this in my rewrite. If you’re entering weight, for example, then you may want to allow going to the tenth, or hundredth, decimal place. Numbers ending with zero that aren’t entered could cause the auto tabbing to not occur. Definitely a feature to consider though.

  19. Great plugin!

    Heep up the good work!

  20. Sepehr permalink

    Thank You ! It’s Very Useful.

  21. Fred permalink

    It does not look like auto-tabbing is working on iPhone 3 and 4. Should it work?

    • Matthew permalink

      I have no clue, but I’d be willing to accept an iPhone, iPod Touch or iPad to test :D

  22. sarfaraz permalink

    How can use this plugin along with live(). I add few fields which are added after page is loaded.

    • Matthew permalink

      That’s an excellent question. You wouldn’t want to run autotab_magic() as that would effectively double, triple, quadruple, etc. the events, and that would be terrible. If you’re concern is maintaining auto tabbing between input fields, you could do the work manually with autotab():

      // targetName		string name of target element
      // previousName	string name of previous element
      // inputFormat		format of input (text, alpha, numeric, custom, etc.)
      // regPattern		regular expression for custom format
      function addField(targetName, previousName, inputFormat, regPattern) {
      	$('input').autotab({ target: targetName, previous: previousName, format: inputFormat, pattern: regPattern });
      }
      

      If you’re looking for a more dynamic approach without having to specify precise elements, that can vary depending on the layout of your code, but jQuery’s selectors should be more than adequate to determine what elements are part of the fold. This example has reminded me that I really need to rewrite this script and make it more modular and less prone to awkward implementations for scenarios such as this.

  23. Jeremy Roy permalink

    Hi

    A great plugin. However I am surprised to see that noone is speaking about a small bug in the code (unless I am missing something).

    To reproduce: The phone field above takes numbers only. If I type ‘a’, it gets deleted and cursor goes back to the first position. However, on the last number if I type a, a gets deleted but the cursor moves to the next input, whereas, the if the letter getting deleted, the cursor should stay in the same input.

    In short, try typing a, 5a, 56a in the first input of the phone field.

    • Matthew permalink

      That behavior sounds likely given that you aren’t prevented from typing characters that aren’t accepted. There are some design flaws in the script and this is one of the things I need to specifically test against when I rewrite the script.

      Wait, what am I saying? You’re typing it wrong!! :P

  24. Jeremy Roy permalink

    :)

  25. Frank permalink

    Hi Matthew,
    thanks for putting in all the work. I’ve installed your plugin and it works great in general but it seems to have a small bug (unless I am getting something wrong):

    Lets assume you open a webpage, which already has characters applied to an input field or you typed already something into a field and after that, you go back into this field.
    If you now DOUBLE-CLICK!!! on this field, it selects the whole content. Then you type something until it gets to the next field. In that case that next input field would not be SELECTED, it only gets FOCUS, which is an odd behavier.

    It behaves different though, if you are using the TAB key (instead of double-clicking) on your keyboard. In that case it correctly selects the next field.

    Any ideas how to solve this problem?
    Thanks a mill in advance for your help
    smash

  26. samskeyti permalink

    great plugin but i have a problem:

    i write this: $(‘.fieldClassSelector’).autotab_magic().autotab_filter({format:’custom’,pattern:’[^1-5]‘});

    in order to accept only 1 digit from 1 to 5 in a list of consecutive fields.

    what happens is that if i type a digit from 1 to 5 it works correctly. if i type, for example, 7 it deletes the digit from the field (correct, 7 is not accepted) but it tabs to the next field anyway. i want that the focus remains in the field that is still empty.

    did i make a mistake?

    • Matthew permalink

      Sounds like it’s a bug Autotab :-\

    • Jorge permalink

      Hi samskeyti,
      I think I may know what the problem is and how to work it around. I was trying to use your code, and it worked fine for me. Then I tweaked my code until it started behaving as you described. The problem started when I placed the formatting statement before the custom filtering statement, like this:

      $(‘.fieldClassSelector’).autotab_magic().autotab({target:’next_field’,format:’numeric’});
      $(‘.fieldClassSelector’).autotab_magic().autotab_filter({format:’custom’,pattern:’[^1-5]‘});

      If by chance you did the same thing, the solution is obvious – switch the order of statements and/or remove format:’numeric’ – it’s not needed with custom formatting anyway.

      • Thanx Jorge, I had the exact same problem and you solved it ! Prehabs Matthew, you should write something about that.

  27. savita permalink

    It’s works nice

  28. Laura permalink

    Hi,
    Thanks for your work.
    I’m trying this in IE and it’s not working:
    I get the following error messages:
    (works in firefox though)

    Webpage error details
    Message: Object doesn’t support this property or method
    Line: 4821
    Char: 5
    Code: 0
    URI: http://www.mathachew.com/wp-includes/js/prototype.js?ver=1.6.1

    Message: Object doesn’t support this property or method
    Line: 25
    Char: 264
    Code: 0
    URI: http://www.mathachew.com/wp-includes/js/jquery/jquery.js?ver=1.4.4

    Any ideas how we could make it work in IE.

    Thanks,
    Laura

    • Matthew permalink

      Sounds like there’s a conflict between jQuery and Prototype since you’re getting a prototype error. Have you tried using the script without prototype loaded? This may break other parts of your site depending how how you’re using it.

  29. Jorge permalink

    Thank you for the great work. Could you please give another example of the use of regular expresions? I am seeing that you are using a subset of the common regular expresion functionality. I am trying to build a textbox with direction, like SW, NE, W, etc. In the world of common regular expressions, I would use something like ^[NS]?[EW]?$/i, but it won’t work here. Is there a way to leverage the autotab plugin for that?
    Thanks.

  30. Vincent permalink

    Hi,

    I use your (wonderfull) plugin with jquery tools’s tooltips.
    It seams the autotab doesn’t work with the tooltips events onBlur because
    the tooltips stay visible after the switch of input.
    Any idee to fix that ?

    Thanks.

  31. Andreas permalink

    Thank you for this great plugin, but I have something to moan about :-)

    As Frank already said, when switching to another field the contents of the field should get highlighted. Just like it gets highlighted when using the TAB key. This does already work in Chrome, but it does not in IE. The destination field only get the focus but the contents is not highlighted.

  32. Ged Byrne permalink

    Stumbled on this when attempting to resolve users pressing ENTER. I didn’t want the form to submit, but I did want the focus to move to the next field. Eventually you’ll reach the submit button where the default behaviour of submitting is adhered to.

    The example below is only for text fields, but it is easily extended for other input types.

    Hope this helps someone.

    $(document).ready(function() {
    $(‘:text’).keypress(function(e) {
    if(e.which == 13) {
    e.preventDefault();
    jQuery(this).moveNext();
    }
    });
    });

  33. Great Plugin, This would be a helpful tweak. Pasting a phone number into the phone field or any correctly formed phrase into a multi part phrase field, and having it all filled in.

    Regards
    -Ron

    • Matthew permalink

      It’s in the plans, I just have not had the chance to really work on this plugin like I want. It really needs to be redone because the features are poorly implemented when wanting to use them at the same time.

  34. Gabriel permalink

    Great plugin!
    Ged Byrne’s post was very helpful too, but I think he forgot to mention that his code depends on another plugin called jquery.field

  35. ReynierPM permalink

    How I can activate the AutoTab feature with multiples values? I mean I have one field that can have 10 or 13 characters and two are valid options for autotab. Any help?

    • Matthew permalink

      Can the field have either 10 or 13 at any given time or only 10 or 13 depending on another condition?

  36. Jeff permalink

    Alien, I wish you would have posted the “small yet stupid mistake” you mentioned. I’ve gone through everything, but keep getting an error that the function is undefined. I even put in my own function within the autotab.js file just to make sure it was loading correctly.

    The code and examples seem very straightforward, so I must be overlooking something “small yet stupid.” Has anyone else gotten this error when trying to implement?

  37. This looks cool but I am brand new to JavaScript.
    Does this line:

    $(document).ready(function() {

    have other values substitued by the coder.
    or do I actually type the word ‘function’ in?
    If so, how does the code connect the two pieces of code?
    ie: form and JS
    And on 3′: What does initialize ‘ready’ mean?
    thanks – Newbie

  38. zai permalink

    Hi i downloaded your plugin but when i implemented it I got this error on line 257 of the auto tab js file “jQuery is not defined”

    Why do i get that? Sorry, im a newbie into this thing so i dont understand much. Any idea how do i fix that?

  39. Vinod permalink

    Hi Matthew,

    I love this plugin. But I can’t get it working on my iphone (its a html5 project), but the same page works like a charm in the desktop browser. May I ask your help with this? I’m willing to work with you to get this working on the iphone (as I have access to a mac and an iphone). It will be really of a great use if you could pull this off. Thanks a lot.

    Vinod.

  40. Vinod permalink

    Thanks Matthew!

  41. Thanks for sharing! Plugin worked great and was easy to set up!

  42. Fantastic plugin. Is it possible to make paste act more intelligently?

    I have a form with several text inputs for entering a code in XXXX-XXXX-XXXX form. If I type the code then autotabbing between the inputs is fine. But, if I paste a long text string, it fills the first box then stops.

    Is there a way around this?

    Thanks.

    • Matthew permalink

      I plan to include this when I rewrite the plugin, and hopefully that will be soon.

  43. Mike Jongbloet permalink

    Any further news on the above ‘intelligent paste’ being added.

    Plugin is great – this function would make it perfect.

    • Matthew permalink

      Only that it’s in the plans for my next update. No eta on when the next version will be available, unfortunately.

      • Mike Jongbloet permalink

        Glad you’re still considering adding it. I attempted expanding your plugin myself but didn’t get very far!

        In the meantime, have you come across any jquery plugins that provide this functionality?

  44. Maicon Voltolini permalink

    Dont work with live() event. Any idea?

  45. kotei permalink

    I want to know when you say jquery.autotab-1.1b, if the “b” is a beta version or another notation.

  46. bunny permalink

    Firstly, thank you for developing this plug-in!

    I was hoping to have: autotab_filter(‘email’) available as a standard feature, but it appears as if you have not rolled that functionality in yet. I’m trying to implement it on a form that I am working on, and I entered the following:

    $(‘#email’).autotab_filter({format:’custom’,pattern:’/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/’});

    Console log is complaining:
    invalid range in character class
    var pattern = new RegExp(defaults.pattern, ‘g’);

    The RegEx statement I copied from another project that uses a javascript ReEx test, so I know that it’s valid.

    Any clues? :)

    • bunny permalink

      p.s. I also tried it without the preceding and trailing backslashes and got the same error.

      $(‘#email’).autotab_filter({format:’custom’,pattern:’^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$’});

      And, congrats on the new-born!!!

  47. Víctor Hernández permalink

    Hi! It’s a great pluging!

    Help me! How can I do that autotab works well when some input text is disabled.

    For example: I have 5 input text in my form, but the input text 3 is disabled, when i finished to write the input text 2 the autotab don’t work. I need that the input text 3 has the focus.

    Thank You!!!

  48. Warren permalink

    I have a page with more than one firm with the same element names. Call them form1 and form2 each of which has field1 and field2. How do I format the ready function to work with both sets of fields?

  49. Santhosh T G V R S permalink

    Hi,

    This is great..

    But, can this plugin be extended to do auto-tab based on tabIndex(now i need to include all elements in jquery selector) and also to support selectboxes (i.e once i select an option in select, it should autotab to next field) as well.

  50. Very useful & powerful script. Thank you. I do, however, have an issue with users holding the backspace to erase input values. It triggers a history.back() event.!

    • Matthew permalink

      You’re welcome! Odd about the history issue, but that doesn’t happen on my examples above. If the focus is on a non-editable field, hitting backspace would trigger that.

Trackbacks & Pingbacks

  1. jQuery AutoTab Plugin « Jquery Labs
  2. Code snippet » 8 ciekawych i przydatnych pluginów do jQuery
  3. Ultimate List of jQuery Plugins You Should Use on Every Website | Web Designer Aid
  4. Ultimate List of jQuery Plugins You Should Use on Every Website « qeqnes | Designing. jQuery, Ajax, PHP, MySQL and Templates
  5. JavaScript and jQuery: The workflow | Web
  6. 方山子的博客 » 24个实用的 jQuery 插件
  7. 分享24款非常有用的 jQuery 插件 « minvision
  8. [转] jQuery插件分享 | 1个饼
  9. Share of 24 very useful jQuery plugin
  10. 80+ AJAX Resources, Downloads, and Tutorials | Redesigner.org
  11. Autotab: jQuery auto-tabbing and filter plugin For TextBox
  12. 分享24款非常有用的jQuery插件 | 想来网
  13. JQUERY « prubman
  14. 15 Simple & Effective jQuery Plugins that Enhance Your Forms | Bitmag

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS