react-input-mask vs maska vs react-maskedinput
React Input Mask Libraries Comparison
1 Year
react-input-maskmaskareact-maskedinputSimilar Packages:
What's React Input Mask Libraries?

Input masking libraries are essential tools in web development that help format user input in real-time, ensuring that data entered into forms adheres to specific patterns. These libraries enhance user experience by guiding users on how to fill out fields correctly, thus reducing errors and improving data integrity. They are particularly useful for fields like phone numbers, dates, and credit card numbers, where a specific format is required. Each of these libraries offers unique features and capabilities, making them suitable for different use cases in React applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-input-mask623,3512,260-1387 years agoMIT
maska187,0141,87555.9 kB416 days agoMIT
react-maskedinput43,904730-627 years agoMIT
Feature Comparison: react-input-mask vs maska vs react-maskedinput

Dynamic Masking

  • react-input-mask:

    React Input Mask supports static masks that are predefined, which can be beneficial for standard formats but lacks the dynamic capabilities that Maska offers. It is more suited for consistent input formats across the application.

  • maska:

    Maska allows for dynamic masking, meaning the mask can change based on user input. This feature is particularly useful for applications that require different formats depending on the context, such as varying phone number formats based on country codes.

  • react-maskedinput:

    React Masked Input provides some level of dynamic masking but is primarily focused on static masks. It allows for customization but may require additional configuration to achieve dynamic behavior.

Customization

  • react-input-mask:

    React Input Mask offers a decent level of customization but is more opinionated in its approach. It provides predefined masks that can be modified, but may not be as flexible as Maska for unique scenarios.

  • maska:

    Maska is highly customizable, allowing developers to define their own mask patterns and behaviors easily. This flexibility makes it suitable for applications with unique input requirements.

  • react-maskedinput:

    React Masked Input excels in customization, allowing developers to create complex input scenarios with detailed control over how the mask behaves. This makes it a great choice for applications needing intricate input handling.

Community Support

  • react-input-mask:

    React Input Mask has a larger community and extensive documentation, making it easier for developers to find help and resources. This can be crucial for teams needing quick solutions and support during development.

  • maska:

    Maska has a smaller community compared to the other two libraries, which may result in less available resources and examples. However, it is straightforward enough that many developers find it easy to implement without extensive documentation.

  • react-maskedinput:

    React Masked Input has a moderate level of community support. While it may not be as extensive as React Input Mask, it still has a dedicated user base and sufficient documentation for most use cases.

Learning Curve

  • react-input-mask:

    React Input Mask has a moderate learning curve, particularly for developers who are new to input masking concepts. However, its comprehensive documentation helps mitigate this issue.

  • maska:

    Maska is designed to be simple and intuitive, making it easy for developers to get started quickly. Its straightforward API allows for rapid implementation without a steep learning curve.

  • react-maskedinput:

    React Masked Input may present a steeper learning curve due to its advanced customization options. Developers may need to invest more time to fully understand its capabilities and how to implement them effectively.

Performance

  • react-input-mask:

    React Input Mask is also performant but may introduce some overhead due to its more extensive feature set. It is generally efficient but could be less optimal in scenarios requiring high performance.

  • maska:

    Maska is lightweight and optimized for performance, making it a good choice for applications where speed is a priority. Its minimal footprint ensures that it does not significantly impact the overall performance of the application.

  • react-maskedinput:

    React Masked Input can be more resource-intensive, especially when handling complex masking scenarios. Developers should be mindful of performance implications when using this library in large applications.

How to Choose: react-input-mask vs maska vs react-maskedinput
  • react-input-mask:

    Opt for React Input Mask if you require a more robust solution with extensive documentation and community support. It is ideal for applications that need a variety of input formats and offers a straightforward API for managing input masks.

  • maska:

    Choose Maska if you need a simple, lightweight solution that supports dynamic masks and is easy to integrate into your React application. It is particularly useful for scenarios where you want to create custom input masks on the fly based on user input.

  • react-maskedinput:

    Select React Masked Input if you are looking for a library that provides a high degree of customization and flexibility. It is suitable for developers who want to create complex input scenarios and need fine-grained control over the masking behavior.

README for react-input-mask

react-input-mask

Build Status npm version npm downloads

Input masking component for React. Made with attention to UX. Compatible with IE8+.

Demo

Table of Contents

Install

npm install react-input-mask --save

Also you can use it without a module bundler

<!-- Load React first -->
<script src="https://unpkg.com/react/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom/dist/react-dom.min.js"></script>
<!-- Will be exported to window.ReactInputMask -->
<script src="https://unpkg.com/react-input-mask/dist/react-input-mask.min.js"></script>

Properties

mask : string

Mask string. Default format characters are:
9: 0-9
a: A-Z, a-z
*: A-Z, a-z, 0-9

Any character can be escaped with a backslash. It will appear as a double backslash in JS strings. For example, a German phone mask with unremoveable prefix +49 will look like mask="+4\9 99 999 99" or mask={'+4\\9 99 999 99'}

maskChar : string

Character to cover unfilled parts of the mask. Default character is "_". If set to null or empty string, unfilled parts will be empty as in ordinary input.

formatChars : object

Defines format characters with characters as a keys and corresponding RegExp strings as a values. Default ones:

{
  '9': '[0-9]',
  'a': '[A-Za-z]',
  '*': '[A-Za-z0-9]'
}

alwaysShowMask : boolean

Show mask when input is empty and has no focus.

inputRef : function

Use inputRef instead of ref if you need input node to manage focus, selection, etc.

Experimental :fire:

The following props are considered experimental because they are more prone to issues and are likely to be changed in the future. Use with caution.

beforeMaskedValueChange : function

In case you need to implement more complex masking behavior, you can provide beforeMaskedValueChange function to change masked value and cursor position before it will be applied to the input. beforeMaskedValueChange receives following arguments:

  1. newState (object): New input state. Contains value and selection fields. selection is null on input blur or when function is called before input mount. Example: { value: '12/1_/____', selection: { start: 4, end: 4 } }
  2. oldState (object): Input state before change. Contains value and selection fields. selection is null on input focus or when function is called before input mount.
  3. userInput (string): Raw entered or pasted string. null if user didn't enter anything (e.g. triggered by deletion or rerender due to props change).
  4. maskOptions (object): Mask options. Example:
{
  mask: '99/99/9999',
  maskChar: '_',
  alwaysShowMask: false,
  formatChars: {
    '9': '[0-9]',
    'a': '[A-Za-z]',
    '*': '[A-Za-z0-9]'
  },
  permanents: [2, 5] // permanents is an array of indexes of the non-editable characters in the mask
}

beforeMaskedValueChange must return an object with following fields:

  1. value (string): New value.
  2. selection (object): New selection. If selection in newState argument is null, it must be null too.

Please note that beforeMaskedValueChange executes more often than onChange and must be pure.

children : function

NOTE: To make this feature more reliable, please tell about your use case in this issue

To use another component instead of regular <input /> pass render function as a children. Function receives props argument which contains props that aren't used by react-input-mask's internals. I.e. it passes down every prop except the following ones: onChange, onPaste, onMouseDown, onFocus, onBlur, value, disabled, readOnly. These properties, if used, should always be passed directly to react-input-mask instead of children and shouldn't be altered in chldren's function.

import React from 'react';
import InputMask from 'react-input-mask';
import MaterialInput from '@material-ui/core/Input';

// Will work fine
const Input = (props) => (
  <InputMask mask="99/99/9999" value={props.value} onChange={props.onChange}>
    {(inputProps) => <MaterialInput {...inputProps} type="tel" disableUnderline />}
  </InputMask>
);

// Will throw an error because InputMask's and children's onChange aren't the same
const InvalidInput = (props) => (
  <InputMask mask="99/99/9999" value={props.value}>
    {(inputProps) => <MaterialInput {...inputProps} type="tel" disableUnderline onChange={props.onChange} />}
  </InputMask>
);

Examples

import React from 'react';
import InputMask from 'react-input-mask';

class PhoneInput extends React.Component {
  render() {
    return <InputMask {...this.props} mask="+4\9 99 999 99" maskChar=" " />;
  }
}

Mask for ZIP Code. Uses beforeMaskedValueChange to omit trailing minus if it wasn't entered by user:

import React from 'react';
import InputMask from 'react-input-mask';

class Input extends React.Component {
  state = {
    value: ''
  }

  onChange = (event) => {
    this.setState({
      value: event.target.value
    });
  }

  beforeMaskedValueChange = (newState, oldState, userInput) => {
    var { value } = newState;
    var selection = newState.selection;
    var cursorPosition = selection ? selection.start : null;

    // keep minus if entered by user
    if (value.endsWith('-') && userInput !== '-' && !this.state.value.endsWith('-')) {
      if (cursorPosition === value.length) {
        cursorPosition--;
        selection = { start: cursorPosition, end: cursorPosition };
      }
      value = value.slice(0, -1);
    }

    return {
      value,
      selection
    };
  }

  render() {
    return <InputMask mask="99999-9999" maskChar={null} value={this.state.value} onChange={this.onChange} beforeMaskedValueChange={this.beforeMaskedValueChange} />;
  }
}

Known Issues

Autofill

Browser's autofill requires either empty value in input or value which exactly matches beginning of the autofilled value. I.e. autofilled value "+1 (555) 123-4567" will work with "+1" or "+1 (5", but won't work with "+1 (___) ___-____" or "1 (555)". There are several possible solutions:

  1. Set maskChar to null and trim space after "+1" with beforeMaskedValueChange if no more digits are entered.
  2. Apply mask only if value is not empty. In general, this is the most reliable solution because we can't be sure about formatting in autofilled value.
  3. Use less formatting in the mask.

Please note that it might lead to worse user experience (should I enter +1 if input is empty?). You should choose what's more important to your users — smooth typing experience or autofill. Phone and ZIP code inputs are very likely to be autofilled and it's a good idea to care about it, while security confirmation code in two-factor authorization shouldn't care about autofill at all.

Thanks

Thanks to BrowserStack for the help with testing on real devices