Convert Figma logo to code with AI

uNmAnNeR logoimaskjs

vanilla javascript input mask

4,979
260
4,979
63

Top Related Projects

Input Mask plugin

17,957

Format input text content when you are typing...

Input mask for React, Angular, Ember, Vue, & plain JavaScript

A jQuery Plugin to make masks on form fields and HTML elements.

jQuery Masked Input Plugin

Quick Overview

IMask.js is a vanilla JavaScript input mask library that provides a flexible and customizable way to format and validate user input in form fields. It supports various input types, including numbers, dates, and custom patterns, and works with both plain inputs and contenteditable elements.

Pros

  • Lightweight and dependency-free
  • Supports a wide range of input types and patterns
  • Works with both input elements and contenteditable fields
  • Highly customizable with extensive options

Cons

  • Learning curve for complex masking patterns
  • Limited built-in internationalization support
  • May require additional setup for framework integration
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Basic number masking:
const element = document.getElementById('phone');
const maskOptions = {
  mask: '+{1}(000)000-0000'
};
IMask(element, maskOptions);
  1. Date masking:
const element = document.getElementById('date');
const maskOptions = {
  mask: Date,
  min: new Date(1900, 0, 1),
  max: new Date(2020, 0, 1),
  lazy: false
};
IMask(element, maskOptions);
  1. Custom function masking:
const element = document.getElementById('custom');
const maskOptions = {
  mask: function (value) {
    return /^[a-z]{0,5}$/i.test(value);
  }
};
IMask(element, maskOptions);

Getting Started

To use IMask.js in your project, follow these steps:

  1. Install IMask.js via npm:

    npm install imask
    
  2. Import IMask in your JavaScript file:

    import IMask from 'imask';
    
  3. Create a mask for an input element:

    const element = document.getElementById('phone');
    const mask = IMask(element, {
      mask: '+{1}(000)000-0000'
    });
    
  4. Access the unmasked value:

    console.log(mask.unmaskedValue);
    

For more advanced usage and options, refer to the official documentation.

Competitor Comparisons

Input Mask plugin

Pros of Inputmask

  • More extensive documentation and examples
  • Wider browser compatibility, including older versions
  • Larger community and more frequent updates

Cons of Inputmask

  • Slightly larger file size
  • More complex setup for some use cases
  • Less flexible for custom mask patterns

Code Comparison

Inputmask:

Inputmask("99/99/9999").mask(document.getElementById("date"));

imaskjs:

IMask(document.getElementById('date'), {
  mask: Date,
  min: new Date(1900, 0, 1),
  max: new Date(2020, 0, 1)
});

Both libraries offer similar functionality for basic input masking, but imaskjs provides more flexibility for complex date masking out of the box. Inputmask requires additional configuration for similar date range functionality.

Inputmask is generally easier to set up for simple use cases, while imaskjs offers more advanced features with a slightly steeper learning curve. Inputmask has been around longer and has a larger user base, which can be beneficial for finding solutions to common issues. However, imaskjs is more lightweight and may be preferred for projects where file size is a concern.

Ultimately, the choice between these libraries depends on specific project requirements, desired features, and performance considerations.

17,957

Format input text content when you are typing...

Pros of Cleave.js

  • Simpler API and easier to set up for basic input masking needs
  • Lightweight and focused specifically on input formatting
  • Better documentation and examples for quick implementation

Cons of Cleave.js

  • Less flexible for complex masking scenarios
  • Limited support for dynamic masks and custom validation
  • Fewer options for handling different input types (e.g., number, date)

Code Comparison

Cleave.js:

new Cleave('.input-phone', {
    phone: true,
    phoneRegionCode: 'US'
});

IMask:

IMask(document.querySelector('.input-phone'), {
    mask: '+{1}(000)000-0000'
});

Both libraries offer straightforward ways to implement input masking, but IMask provides more granular control over the mask pattern. Cleave.js uses predefined formats, while IMask allows for custom mask definitions.

IMask offers more advanced features like dynamic masking, custom validation, and support for various input types. However, this comes at the cost of a slightly steeper learning curve and more complex setup for advanced use cases.

Cleave.js shines in its simplicity and ease of use for common input formatting scenarios, making it a good choice for projects with straightforward masking requirements. IMask is better suited for applications that need more flexibility and control over input masking and validation.

Input mask for React, Angular, Ember, Vue, & plain JavaScript

Pros of text-mask

  • More mature project with a larger community and better documentation
  • Supports multiple frameworks out of the box (React, Angular, Vue, etc.)
  • Simpler API for basic use cases

Cons of text-mask

  • Less flexible for complex masking scenarios
  • Not actively maintained (last commit was in 2018)
  • Limited options for customization compared to imaskjs

Code Comparison

text-mask:

import createNumberMask from 'text-mask-addons/dist/createNumberMask'

const numberMask = createNumberMask({
  prefix: '$',
  allowDecimal: true
})

<MaskedInput
  mask={numberMask}
  guide={false}
  placeholderChar={'\u2000'}
  onChange={this.onChange}
/>

imaskjs:

import IMask from 'imask';

const maskOptions = {
  mask: Number,
  scale: 2,
  signed: false,
  thousandsSeparator: ',',
  padFractionalZeros: false,
  normalizeZeros: true,
  radix: '.',
  mapToRadix: ['.']
};

const mask = IMask(element, maskOptions);

Both libraries offer input masking functionality, but imaskjs provides more granular control over the masking process. text-mask is easier to set up for simple cases but may be limited for complex scenarios. imaskjs is actively maintained and offers more flexibility, while text-mask has better framework integration out of the box.

A jQuery Plugin to make masks on form fields and HTML elements.

Pros of jQuery-Mask-Plugin

  • Lightweight and easy to use, especially for developers familiar with jQuery
  • Extensive documentation and examples available
  • Wide browser compatibility, including older versions

Cons of jQuery-Mask-Plugin

  • Dependency on jQuery, which may not be ideal for modern web applications
  • Limited flexibility for complex masking scenarios
  • Less active development and fewer recent updates

Code Comparison

jQuery-Mask-Plugin:

$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#money').mask('000.000.000.000.000,00', {reverse: true});

imaskjs:

IMask(document.getElementById('date'), {
  mask: Date,
  min: new Date(1990, 0, 1),
  max: new Date(2020, 0, 1)
});
IMask(document.getElementById('phone'), {
  mask: '+{7}(000)000-00-00'
});

imaskjs offers more advanced masking options and doesn't rely on jQuery. It provides a more modern approach to input masking with support for various input types and complex patterns. However, it may have a steeper learning curve compared to jQuery-Mask-Plugin.

Both libraries serve their purpose well, but imaskjs is more suitable for modern web development practices and complex masking requirements, while jQuery-Mask-Plugin remains a solid choice for simpler use cases in jQuery-based projects.

jQuery Masked Input Plugin

Pros of jquery.maskedinput

  • Lightweight and simple to use, especially for jQuery-based projects
  • Well-established with a long history of development and community support
  • Extensive documentation and examples available

Cons of jquery.maskedinput

  • Dependency on jQuery, which may not be ideal for modern web applications
  • Less flexible for complex input masking scenarios
  • Limited support for non-input elements and dynamic mask changes

Code Comparison

jquery.maskedinput:

$("#phone").mask("(999) 999-9999");
$("#date").mask("99/99/9999");

imaskjs:

IMask(document.getElementById('phone'), {
  mask: '(000) 000-0000'
});
IMask(document.getElementById('date'), {
  mask: Date
});

imaskjs offers more flexibility and advanced features, such as custom mask patterns and dynamic masks. It also works with vanilla JavaScript, making it more versatile for modern web development. However, jquery.maskedinput may be easier to implement for developers already using jQuery and requiring simple input masking functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

imaskjs

vanilla javascript input mask

npm version License: MIT Coverage Status

Features

  • get and set raw and unmasked values easily
  • no external dependencies
  • supports overwrite mode
  • supports web components
  • supports contenteditable
  • RegExp mask
  • Function mask
  • Number mask (integer and decimal)
  • Date mask (various format support, autofix mode)
  • Dynamic/on-the-fly mask
  • Pattern mask
    • show placeholder always and only when necessary
    • unmasked value can contain fixed parts
    • optional input parts (greedy)
    • custom definitions
    • reusable blocks
    • secure text entry
    • Enum and Range masks
    • Repeating blocks
  • convert and format values with pipe

Plugins

Install

npm install imask and import IMask from 'imask';

or use CDN:

<script src="https://unpkg.com/imask"></script>

Build & Test

npm run make

Compatibility

Supports all major browsers. Can also be used with outdated browsers. See how

Getting help

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

NPM DownloadsLast 30 Days