Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Mathachew/jquery-autotab

A jQuery plugin that provides auto tabbing and filtering on text fields in a form
https://github.com/Mathachew/jquery-autotab

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

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](http://autotab.mathachew.com/angular.html) and [Knockout](http://autotab.mathachew.com/knockout.html) demos are also available.

## Table of Contents
* [Requirements](#requirements)
* [Installation](#installation)
* [Setup and Usage](#setup-and-usage)
* [Auto Tabbing](#auto-tabbing)
* [Examples](#examples)
* [Remove and Restore](#remove-and-restore)
* [Examples](#examples-1)
* [Filtering](#filtering)
* [Examples](#examples-2)
* [Global Methods](#global-methods)
* [Options](#options)
* [Filter Formats](#filter-formats)
* [Known Issues](#known-issues)
* [Minify](#minify)
* [Feedback](#feedback)
* [Copyright and License](#copyright-and-license)

## 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.

```html

```

## 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.

```js
$('.answer').autotab({ maxlength: 1 });
```
```html


Answer 1
-
Answer 2
-
Answer 3
-

```

Automatically establishes auto tabbing order and number filtering.

```js
$('.number').autotab('number');
```
```html


Phone Number
-
-


```

Manually defines auto tabbing order and alphanumeric filtering.

```js
$('#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' });
```
```html


Product Key
-
-
-
-


```

### 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.

```js
$('#remove-autotab').on('click', function () {
$('input[type=text]').autotab('remove');
});
```
```html
Turn Off
```

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

```js
$('#restore-autotab').on('click', function () {
$('input[type=text]').autotab('restore');
});
```
```html
Turn On
```

### 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.

```js
$('#salt').autotab('filter', 'text');
```
```html


Salt


```

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

```js
$('.alphanumeric').autotab('filter', { format: 'alphanumeric', uppercase: true });
```
```html


Product Key
-
-
-
-


```

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

```html


Phone Number
-
-


```

Other random JavaScript examples

```js
$(':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

```js
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](http://closure-compiler.appspot.com/) (`simple` optimization) to create jquery.autotab.min.js.

## Feedback

Please provide feature requests and bug reports here on [GitHub](../../issues).

You can also reach out to me on twitter: [@mathachew](http://www.twitter.com/mathachew)

## Copyright and license

© 2015 Matthew Miller

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