Home / js
Name Modified Size InfoDownloads / Week
Parent folder
jquery.autotab.min.js 2013-11-04 5.0 kB
jquery.autotab.js 2013-11-04 16.1 kB
Totals: 2 Items   21.1 kB 0

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.
  • It is small, fast, easy to load and built on the powerful jQuery library.

Pasting support has basic functionality and has a lot of room for improvement, so use at your own risk.

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: Defines the filter format that will be used on all matched elements.
.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>

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.

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>

Other random JavaScript examples

$('input[type=text]').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');

Options

var options = {
    format: string|function,
    pattern: string,
    uppercase: boolean,
    lowercase: boolean,
    nospace: boolean,
    maxlength: integer,
    target: string|element,
    previous: string|element
};
format string: Speficies which format rule to use on a text box's value.
function: If a single regular expression is insufficient, a function can be used for custom formatting.
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.

Filter Formats

Autotab has several filter formats available. If none of the formats meet your needs, Autotab also supports a custom option where you can pass either a regular expression or a function.

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

Feedback

Please provide feature requests and bug reports here on GitHub.

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

© 2013 Matthew Miller

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

Source: README.md, updated 2013-11-04