• Stars
    star
    367
  • Rank 116,257 (Top 3 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A jQuery plugin that provides auto tabbing and filtering on text fields in a form

jQuery Autotab

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 text field, the focus is automatically set to a defined element. Likewise, clearing out the text field's content by pressing backspace eventually places the focus on a previous element.

Why jQuery Autotab?

  • Auto tabbing behaves logically, in both tabbing forward and tabbing backwards.
  • Allow your users to easily modify your text in a tab that would otherwise auto tab away.
  • Reduce the amount of bad data submitted in a form by filtering text fields.
  • Populate multiple text fields by pasting into one.
  • Enhance text fields by auto tabbing when a specific character is pressed.
  • It is small, fast, easy to load and built on the powerful jQuery library.

Demo

Always running the latest and greatest version of Autotab: http://autotab.mathachew.com/

Angular and Knockout demos are also available.

Table of Contents

Requirements

Autotab works in both jQuery 1.7+ and 2.x. If your site supports Internet Explorer 6, 7, and/or 8, use jQuery 1.7+ since 2.x drops support for these browsers.

Installation

Add a reference to jquery.autotab.js.

<script src="jquery.autotab.js"></script>

Setup and Usage

Autotab can be setup in several different ways within jQuery's $(document).ready() event. The two components that make up Autotab, auto tabbing and filtering, can be managed independently from one another, providing numerous ways of achieving the same result, depending on how indepth you want to setup your form.

Auto Tabbing

Note: If the selector matches multiple elements, the target and previous fields will be overwritten if previously set.

.autotab() Accepts no arguments and applies auto tabbing rules only.
.autotab(string) string: Can be a filter format or a value that tells the script to remove or restore auto tab and filtering functionality.
Note: Previous auto tabbing order will be overwritten. To change the filter only, call .autotab('filter', '')
.autotab(object) object: Applies the specified options to all matched elements.

Examples

Establishes auto tabbing rules only and forces each field to have a maxlength of 1.

$('.answer').autotab({ maxlength: 1 });
<div>
    <label>Answer 1</label>
    <input type="text" id="answer1" class="answer" size="3" /> -
    <label>Answer 2</label>
    <input type="text" id="answer2" class="answer" size="3" /> -
    <label>Answer 3</label>
    <input type="text" id="answer3" class="answer" size="3" /> -
</div>

Automatically establishes auto tabbing order and number filtering.

$('.number').autotab('number');
<div>
    <label>Phone Number</label>
    <input type="text" id="number1" class="number" maxlength="3" size="3" /> -
    <input type="text" id="number2" class="number" maxlength="3" size="3" /> -
    <input type="text" id="number3" class="number" maxlength="4" size="5" />
</div>

Manually defines auto tabbing order and alphanumeric filtering.

$('#alphanumeric1').autotab({ format: 'alphanumeric', target: '#alphanumeric2' });
$('#alphanumeric2').autotab({ format: 'alphanumeric', target: '#alphanumeric3', previous: '#alphanumeric1' });
$('#alphanumeric3').autotab({ format: 'alphanumeric', target: '#alphanumeric4', previous: '#alphanumeric2' });
$('#alphanumeric4').autotab({ format: 'alphanumeric', target: '#alphanumeric5', previous: '#alphanumeric3' });
$('#alphanumeric5').autotab({ format: 'alphanumeric', previous: '#alphanumeric4' });
<div>
    <label>Product Key</label>
    <input type="text" id="alphanumeric1" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric2" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric3" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric4" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric5" class="alphanumeric" maxlength="5" size="4" />
</div>

Remove and Restore

.autotab('remove|destroy|disable') string: Disables auto tab and filtering functionality on all matched elements.
Note: Both destroy and remove will disable auto tab and filtering. Standard and custom event bindings persist as they check the status of an element when called. Removing the keydown and keypress can have a negative impact in both user and developer experience if the same events have been bound in other areas.
.autotab('restore|enable') string: Restores auto tab and filtering functionality on all matched elements.

Examples

Manually turn off auto tab and filtering functionality on all text boxes.

$('#remove-autotab').on('click', function () {
    $('input[type=text]').autotab('remove');
});
<button id="remove-autotab">Turn Off</button>

Manually turn on auto tab and filtering functionality on all text boxes.

$('#restore-autotab').on('click', function () {
    $('input[type=text]').autotab('restore');
});
<button id="restore-autotab">Turn On</button>

Filtering

Note: If passing an object, the target and previous fields are ignored, if included, to preserve previously established auto tab rules. Use .autotab(object) to modify the target and previous fields.

.autotab('filter', string) string: Applies the specified filter format to all matched elements.
.autotab('filter', object) object: Applies the specified filter options to all matched elements. The target and previous fields are ignored.

Because of how Autotab's settings are stored, it is possible to define the filter format using data-autotab-format. If using custom, place your regular expression in data-autotab-pattern.

It is possible to specify the filter through an element's class, using the same names that are available when calling the filter. In order to use this feature, $.autotab.selectFilterByClass must be set to true before initializing Autotab.

Examples

Manually defines text filtering.

$('#salt').autotab('filter', 'text');
<div>
    <label>Salt</label>
    <input type="text" id="salt" maxlength="16" size="12" />
</div>

Manually defines alphanumeric filtering and converts all alpha characters to uppercase format.

$('.alphanumeric').autotab('filter', { format: 'alphanumeric', uppercase: true });
<div>
    <label>Product Key</label>
    <input type="text" id="alphanumeric1" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric2" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric3" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric4" class="alphanumeric" maxlength="5" size="4" /> -
    <input type="text" id="alphanumeric5" class="alphanumeric" maxlength="5" size="4" />
</div>

Manually defines number filtering via data-autotab-format. In this example, $(selector).autotab() will take the attribute into account.

<div>
    <label>Phone Number</label>
    <input type="text" id="phone1" maxlength="3" size="3" data-autotab-format="number" /> -
    <input type="text" id="phone2" maxlength="3" size="3" data-autotab-format="number" /> -
    <input type="text" id="phone3" maxlength="4" size="4" data-autotab-format="number" />
</div>

Other random JavaScript examples

$(':input').autotab();
$('#username').autotab({ format: 'alphanumeric', target: '#password' });
$('#password').autotab({ previous: '#username', target: '#confirm' });
$('#product-key').autotab('filter', { format: 'alphanumeric', uppercase: true });
$('#ip-address').autotab('filter', { format: 'custom', pattern: '[^0-9\.]' });
$('#function').autotab('filter', function (text) { alert(text); });
$('#number1, #number2, #number3').autotab('filter', 'number');
$('.ipv6').autotab('filter', 'hexadecimal');

Global Methods

Autotab comes with several global methods, which are probably most useful in edge cases.

$.autotab() Initializes Autotab on all elements matching the :input selector.
$.autotab(object) object: Applies the specified options to all matched elements.
$.autotab.next() Triggers the `autotab-next` event, which sets the focus on the target element, if it exists.
Note: This method is only useful if an element setup with Autotab has focus.
$.autotab.previous() Triggers the `autotab-previous` event, which sets the focus on the previous element, if it exists.
Note: This method is only useful if an element setup with Autotab has focus.
$.autotab.remove() Removes Autotab from all matched elements.
$.autotab.remove(string) string: A selector identifying the matched element(s).
$.autotab.remove(object) object: Applies the removal to all matched elements.
$.autotab.restore() Restores Autotab to all matched elements.
$.autotab.restore(string) string: A selector identifying the matched element(s).
$.autotab.restore(object) object: Applies restoration to all matched elements.
$.autotab.refresh() Refreshes the tabbing order on all elements matching the :input selector.
$.autotab.refresh(string) string: A selector identifying the matched element(s)
$.autotab.refresh(object) object: Refreshes the target/previous values for all matched elements.
Under certain conditions, using refresh may cause an unexpected tabbing order, so the :input selector is recommended.

Options

var options = {
    format: string|function,
    pattern: string,
    uppercase: boolean,
    lowercase: boolean,
    nospace: boolean,
    maxlength: integer,
    target: string|element,
    previous: string|element,
    trigger: string|array,
    tabOnSelect: boolean
};
format string: Speficies which format rule to use on a text box's value.
function(value, element): If a single regular expression is insufficient, a function can be used for custom formatting. The parameter value is the typed character and element is the focused JavaScript element.
Note: Go to Filter Formats to see each available format option.
pattern string: Used only when the custom format is specified, and it must be a string.
uppercase boolean: Forces all alpha characters to uppercase format.
lowercase boolean: Forces all alpha characters to lowercase format.
nospace boolean: Determines if spaces are allowed or not.
Note: Space and underscore are not alpha characters and are not included in the alpha and alphanumeric format options. Use the custom format to allow these characters.
maxlength integer: The maximum number of characters allowed in a text box. Maxlength can be specified with the HTML attribute maxlength.
Note: The maxlength attribute value can be overwritten if the maxlength field is passed to autotab().
target When auto tabbing occurs, target is the element that will gain focus.
string: A selector identifying the next element.
element: The JavaScript or jQuery element.
previous When backspacing or reverse tabbing, previous is the element that will gain focus.
string: A selector identifying the next element.
element: The JavaScript or jQuery element.
trigger Triggers autotab-next when the specified characters are pressed.
string: A string of one or more characters
element: An array of one or more strings
tabOnSelect boolean: Adds auto tabbing to all matched single value select lists.

Filter Formats

Autotab has several filter formats available, all passed into the format key. If none of the formats meet your needs, Autotab also supports a passing a function or specifying custom option where you can pass a regular expression.

all Allows any and all characters.
text Allows all characters, including special characters, except numbers.
alpha Allows only letters.
number|numeric Allows only numbers.
alphanumeric Allows only letters and numbers.
hex|hexadecimal Allows only letters A-F, a-f and numbers.
custom Allows a developer to provide a custom regular expression: new RegExp(pattern, 'g');
Note: Requires pattern: string, ie: pattern: "[^0-9\.]"
function(value, element) Allows a developer to provide a their own function in case a regular expression is insufficient.
Note: value is the typed character, element is the focused JavaScript element.

Known Issues

  • Due to security measures placed in iOS, Autotab cannot achieve auto tabbing functionality when hitting a field's character limit. The problem stems from the focus event not being triggered manually. As a workaround, Autotab works with iOS by keeping the keyboard open, allowing you to navigate using the arrow shortcuts.
  • Any script that uses the keydown and keypress events may conflict with Autotab, or vice versa. As of 1.9.0, Autotab uses event extensions in an attempt to prevent this from happening.
  • With limitations of selection in most text field types, only text, password and textarea fields support auto tabbing and filtering, while tel, number, email, url and search support auto tabbing only.
  • Drop events will not work for IE11 since changing the maxlength property causes the event from proceeding. IE6-10 work fine, however.

Minify

Autotab uses the Closure Compiler (simple optimization) to create jquery.autotab.min.js.

Feedback

Please provide feature requests and bug reports here on GitHub.

You can also reach out to me on twitter: @mathachew

Copyright and license

© 2015 Matthew Miller

Licensed under the MIT licensing: http://www.opensource.org/licenses/mit-license.php