Inputmask
Inputmask is a JavaScript library for applying client-side input masks to HTML form elements, ensuring user input conforms to a predefined format such as phone numbers, dates, or currency. It helps guide users, reduces input errors, and improves data consistency. The current stable version is 5.0.9, with updates released as needed for bug fixes, new features, and browser compatibility. A key differentiator is its versatile support for vanilla JavaScript, jQuery, and jqlite, making it adaptable across diverse web projects. The library provides an extensive set of predefined masks and offers robust options for custom mask definitions, including control over placeholders, repeat behavior, and real-time validation, making it a flexible and powerful solution for input formatting.
Common errors
-
TypeError: $(...).inputmask is not a function
cause The jQuery plugin for Inputmask (`jquery.inputmask.js`) was not loaded, or jQuery itself was not available before the plugin tried to attach.fixVerify that jQuery is included on the page, followed by `inputmask.js`, then any extension files (e.g., `inputmask.numeric.extensions.js`), and finally `jquery.inputmask.js`. -
Uncaught ReferenceError: Inputmask is not defined
cause The main Inputmask library script (`inputmask.js`) was not loaded or imported correctly in a vanilla JavaScript setup.fixFor ESM, ensure `import Inputmask from 'inputmask';` is at the top of your module. For script tags, verify the `inputmask.js` file is correctly linked in your HTML before its usage. -
Uncaught ReferenceError: NULL is not defined
cause This specific error was reported when upgrading from Inputmask v5 beta to 5.0.8, indicating an issue with how `null` was handled in certain parts of the library.fixEnsure you are on Inputmask version 5.0.8 or later, as this issue was addressed in that release. If using an older beta, update to the latest stable v5.0.x. -
Mask not applied to input field / Input field not responding to mask pattern
cause The JavaScript code to apply the mask is executing before the target input element exists in the DOM, or the selector used is incorrect.fixWrap your masking logic inside a `DOMContentLoaded` event listener or a `$(document).ready()` block for jQuery. Double-check your element ID or class selectors to ensure they match existing HTML elements.
Warnings
- breaking Version 5.0 introduced stricter typings and some changes to internal behaviors, including how overwrite mode works. Code written for v4 may require adjustments.
- gotcha When using the jQuery plugin (`jquery.inputmask.js`), ensure that jQuery is loaded and available *before* both the main `inputmask.js` and `jquery.inputmask.js` scripts. Incorrect loading order will prevent the plugin from attaching to jQuery.
- gotcha Optional parts in masks, denoted by square brackets `[]`, should generally be placed at the end of the mask pattern. Placing them in the middle can lead to unexpected or inconsistent masking behavior.
- breaking Early beta versions of Inputmask v5 (e.g., v5 beta 131+) were reported to cause conflicts with other JavaScript libraries and break page functionality when certain date formatting options like `inputformat:'yyyy-mm-dd'` were used. This was likely due to aggressive DOM manipulation or global scope pollution.
Install
-
npm install inputmask -
yarn add inputmask -
pnpm add inputmask
Imports
- Inputmask
const Inputmask = require('inputmask');import Inputmask from 'inputmask';
- jQuery Plugin
import { Inputmask } from 'inputmask/dist/jquery.inputmask';import 'jquery'; import 'inputmask/dist/inputmask'; import 'inputmask/dist/jquery.inputmask';
- Type Definitions
import { InputmaskOptions } from 'inputmask';import type { InputmaskOptions } from 'inputmask'; - Specific Extensions (ESM)
import Inputmask from 'inputmask'; import 'inputmask/lib/extensions/inputmask.numeric.extensions';
Quickstart
import Inputmask from 'inputmask';
// Vanilla JavaScript example
document.addEventListener('DOMContentLoaded', () => {
const phoneInput = document.getElementById('phone-number');
const dateInput = document.getElementById('date-input');
if (phoneInput) {
const phoneMask = new Inputmask('+9 (999) 999-9999');
phoneMask.mask(phoneInput);
}
if (dateInput) {
// Ensure inputmask.date.extensions is imported for 'datetime' alias
const dateMask = new Inputmask('datetime', {
inputFormat: 'dd/mm/yyyy',
placeholder: 'dd/mm/yyyy'
});
dateMask.mask(dateInput);
}
});
// HTML to go with the above JavaScript:
// <input type="text" id="phone-number" placeholder="+_ (___) ___-____">
// <input type="text" id="date-input" placeholder="dd/mm/yyyy">