jQuery Mask Plugin

repository·master·Indexed 26 days ago

https://github.com/igorescobar/jquery-mask-plugin

A lightweight jQuery plugin for applying input masks to form fields and HTML elements. It supports string, numeric, alpha, and mixed masks, featuring reverse masking, sanitization, and recursive digits. Compatible with jQuery 1.7+, it provides methods like .mask(), .unmask(), and .cleanVal(), and supports configuration via HTML data attributes or global settings through $.jMaskGlobals.

Tokens
1.1K
Snippets
2
Records
11
Agent score
39%

What's inside jquery-mask-plugin

  1. Install TypeScript definitions for jQuery Mask Plugin

    master

    To use the plugin with TypeScript, install the type definitions based on your TypeScript version:

    For TypeScript 2.x users

    Use npm to install the @types package:

    npm install --save-dev @types/jquery-mask-plugin

    For TypeScript 1.x users

    1. Install typings globally:
    npm install typings --global
    1. Install the definition via typings:
    typings install dt~jquery-mask-plugin --global --save
  2. Install jQuery Mask Plugin via Package Managers

    master

    You can install the jQuery Mask Plugin using various package managers depending on your environment:

    • Bower: bower install jquery-mask-plugin
    • NPM: npm i jquery-mask-plugin
    • Meteor: meteor add igorescobar:jquery-mask-plugin
    • Composer (PHP): composer require igorescobar/jquery-mask-plugin
    • RubyGems: Add gem 'jquery_mask_rails' to your Gemfile.
    npm i jquery-mask-plugin
  3. Use HTML notation for masks

    master

    The plugin supports applying masks directly via HTML data-* attributes, which allows for easy implementation without writing custom JavaScript for every field:

    • data-mask: Defines the mask pattern.
    • data-mask-recursive: Enables recursive masking.
    • data-mask-clearifnotmatch: Clears the field if the input does not match the mask.
  4. Configure masks via HTML data attributes

    master

    The plugin can automatically initialize masks if elements have a data-mask attribute. You can also control behavior using specific data attributes:

    • data-mask="pattern": The mask pattern.
    • data-mask-reverse="true": Enables reverse masking.
    • data-mask-clearifnotmatch="true": Clears value if it doesn't match on blur.
    • data-mask-selectonfocus="true": Selects text on focus.
  5. Configure global settings via $.jMaskGlobals

    master

    You can modify global plugin behavior by setting properties on $.jMaskGlobals.

    Available Global Keys:

    • maskElements: Selector for elements to be automatically masked (default: 'input,td,span,div').
    • dataMaskAttr: Selector for the data attribute used for auto-masking (default: '[data-mask]').
    • dataMask: Boolean. Whether to automatically apply masks to elements with data-mask (default: true).
    • watchInterval: Integer. Interval in ms for watching new elements (default: 300).
    • watchInputs: Boolean. Whether to watch for new inputs (default: true).
    • keyStrokeCompensation: Integer. Delay for caret positioning (default: 10).
    • byPassKeys: Array of key codes to ignore (default: [9, 16, 17, 18, 36, 37, 38, 39, 40, 91]).
    • translation: Object containing character patterns.
  6. Compatibility and Browser Support

    master

    The jQuery Mask Plugin is compatible with jQuery 1.7+ and has been tested on the following browsers:

    • Firefox 2+ (Win, Mac, Linux)
    • IE7+ (Win)
    • Chrome 6+ (Win, Mac, Linux, Android, iPhone)
    • Safari 3.2+ (Win, Mac, iPhone)
    • Opera 8+ (Win, Mac, Linux, Android, iPhone)
    • Android Default Browser v4+
  7. Apply a mask to an element using .mask()

    master

    Use the .mask(mask, options) method on a jQuery object to apply input masking. The mask can be a string defining the pattern or a function that returns a mask string.

    Common Options:

    • placeholder: String to display as a placeholder.
    • selectOnFocus: Boolean. If true, selects all text when the element gains focus.
    • clearIfNotMatch: Boolean. If true, clears the value if it doesn't match the mask on focusout.
    • reverse: Boolean. If true, masks from right to left.
    • translation: Object to override default character translations.
    • onChange, onKeyPress, onComplete, onInvalid: Callback functions.

    Default Translations:

    • '0': /\d/ (Required digit)
    • '9': /\d/ (Optional digit)
    • '#': /\d/ (Recursive digit)
    • 'A': /[a-zA-Z0-9]/ (Alphanumeric)
    • 'S': /[a-zA-Z]/ (Alphabetic)