IMask.js is a robust vanilla JavaScript input mask library that dynamically formats user input in form fields, ensuring data consistency and improving user experience. It supports a wide array of mask types including pattern, number, date, range, enum, and dynamic masks, and allows for custom definitions and repeating blocks. The library is currently in active development, with version 7.6.1 being the latest stable release, featuring frequent minor updates to address bugs and introduce enhancements like autofix options and improved IME support. A key differentiator is its framework-agnostic core, accompanied by dedicated plugins for popular frameworks such as React, Vue, Angular, Svelte, and Solid, enabling consistent masking behavior across various JavaScript ecosystems. It has no external dependencies and is designed for broad browser compatibility.
npm install imaskVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up two common masks: a phone number using a pattern mask and a currency input using a number mask. It highlights basic initialization, options like `lazy` and `placeholderChar`, and how to access `value` and `unmaskedValue`.
Update import paths from `import 'imask/esm/masked/some-mask'` to `import 'imask/masked/some-mask';`.
Ensure your build system correctly handles ESM modules. For CJS environments, explicit `require('imask/cjs/some-path')` might be necessary, though `import IMask from 'imask'` should generally resolve correctly if `package.json` `exports` map correctly.Ensure all HTML input elements intended for `IMask.js` have `type="text"`.
Use side-effect imports for specific masked types: `import IMask from 'imask'; import 'imask/masked/number';`.
Interact with the `IMask` instance (e.g., `mask.updateOptions()`, `mask.value = '...'`) to control the masked input. Use `onAccept` or `onComplete` callbacks for events. If modifying the underlying DOM element directly, ensure changes are synchronized or considered in the context of `IMask`'s internal handling.
When using `RegExp` or `Function` masks, design them to validate intermediate states gracefully. For complex cases, `Pattern` or `Function` masks are often more suitable, or implement custom `validate` callbacks that handle partial inputs.
Ensure `IMask` is imported as `import IMask from 'imask';` at the top of your module or that the `<script src="https://unpkg.com/imask"></script>` tag is placed before any script attempting to use `IMask` in a global context.
Always initialize IMask using the main factory function: `const mask = IMask(element, options);` Do not try to directly construct `IMask.InputMask` or similar internal components. Ensure your bundler is configured correctly if tree-shaking is aggressively removing parts of `imask`.
For React/Vue plugins, ensure you are on the latest minor version. In `v7.6.0`, `onAccept` is now only called on init if the value changes, which might affect validation logic. Always refer to the specific plugin documentation for correct lifecycle and prop handling. Ensure `ref` is passed correctly in React hooks if needed.
Test copy/paste functionality thoroughly with your specific mask configuration. For number masks, ensure `thousandsSeparator` and `radix` options are correctly set to match expected input formats. Consider implementing a custom `prepare` callback to normalize pasted values if default behavior is insufficient.
No dependency data recorded yet.