react-number-format vs react-phone-number-input vs react-input-mask vs react-text-mask
Input Masking and Formatting Libraries for React
react-number-formatreact-phone-number-inputreact-input-maskreact-text-maskSimilar Packages:
Input Masking and Formatting Libraries for React

Input masking and formatting libraries for React are tools that help developers control the input format of text fields in forms. These libraries provide features like masking user input (e.g., phone numbers, credit card numbers) to ensure data is entered in a specific format, which can improve data quality and user experience. They often include customizable masks, formatting options, and validation to handle various input types. Examples include react-input-mask, react-number-format, and react-phone-number-input, each offering unique features for different use cases.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-number-format3,144,8634,073240 kB23210 months agoMIT
react-phone-number-input1,405,395-10.1 MB-3 months agoMIT
react-input-mask660,6902,291-1397 years agoMIT
react-text-mask437,7698,23848.8 kB334-Unlicense
Feature Comparison: react-number-format vs react-phone-number-input vs react-input-mask vs react-text-mask

Input Masking

  • react-number-format:

    react-number-format does not provide traditional input masking but formats the input as the user types. It handles numeric values, including currency and percentages, and ensures that the input adheres to the specified format. This is more about formatting than masking, which can be useful for numeric inputs.

  • react-phone-number-input:

    react-phone-number-input focuses on phone number input. It formats the number as the user types and provides validation to ensure the input is a valid phone number. While it does not offer traditional masking, its formatting and validation features are tailored for phone numbers, making it effective for that use case.

  • react-input-mask:

    react-input-mask provides basic input masking capabilities. You can define masks using a simple syntax, and it supports both static and dynamic masks. This library is lightweight and easy to integrate, making it ideal for simple masking needs.

  • react-text-mask:

    react-text-mask offers flexible input masking by allowing developers to create custom masks using a combination of characters and functions. It provides more control over how the input is masked compared to other libraries, making it suitable for complex masking requirements.

Number Formatting

  • react-number-format:

    react-number-format excels in number formatting. It supports various formats, including currency, percentages, and custom numeric formats. You can easily specify the format, and it handles both input and display formatting, making it ideal for applications that require precise control over how numbers are presented.

  • react-phone-number-input:

    react-phone-number-input does not provide number formatting features beyond phone numbers. It formats the phone number according to the selected country’s format but does not handle other types of numeric formatting.

  • react-input-mask:

    react-input-mask does not handle number formatting. It focuses on masking input based on the defined mask, without any built-in support for formatting numeric values.

  • react-text-mask:

    react-text-mask does not provide number formatting features. It focuses on masking input based on the defined mask, without any built-in support for formatting numeric values.

Internationalization

  • react-number-format:

    react-number-format supports internationalization to some extent, especially for currency and number formats. You can specify different formats based on the locale, but it may require additional configuration for full internationalization support.

  • react-phone-number-input:

    react-phone-number-input is designed with internationalization in mind. It supports phone numbers from various countries, provides a dropdown for selecting the country code, and formats the number according to the selected country’s format, making it highly suitable for global applications.

  • react-input-mask:

    react-input-mask does not provide built-in support for internationalization. It is primarily focused on masking input based on the defined mask, which can be customized for different languages or regions but requires manual configuration.

  • react-text-mask:

    react-text-mask does not provide built-in internationalization features. It focuses on masking input based on the defined mask, which can be customized for different languages or regions but requires manual configuration.

Validation

  • react-number-format:

    react-number-format provides validation for numeric inputs, ensuring that the value adheres to the specified format. It can handle invalid input gracefully and provides callbacks for handling changes and validation errors.

  • react-phone-number-input:

    react-phone-number-input includes validation for phone numbers. It checks if the entered phone number is valid based on the selected country’s format and provides feedback on invalid numbers, making it a reliable choice for applications that require accurate phone number input.

  • react-input-mask:

    react-input-mask does not perform validation on its own. It masks the input based on the defined mask, but you will need to implement your own validation logic to ensure the input meets any specific requirements.

  • react-text-mask:

    react-text-mask does not provide built-in validation. It masks the input based on the defined mask, but you will need to implement your own validation logic to ensure the input meets any specific requirements.

Ease of Use: Code Examples

  • react-number-format:

    Currency Formatting with react-number-format

    import React from 'react';
    import NumberFormat from 'react-number-format';
    
    const CurrencyInput = () => {
      return (
        <NumberFormat
          thousandSeparator
          prefix="$"
          placeholder="Enter amount"
          isNumericString
        />
      );
    };
    
    export default CurrencyInput;
    
  • react-phone-number-input:

    Phone Number Input with Validation using react-phone-number-input

    import React, { useState } from 'react';
    import PhoneInput from 'react-phone-number-input';
    import 'react-phone-number-input/style.css';
    
    const PhoneNumberComponent = () => {
      const [value, setValue] = useState();
      return (
        <PhoneInput
          international
          countryCallingCodeEditable
          value={value}
          onChange={setValue}
        />
      );
    };
    
    export default PhoneNumberComponent;
    
  • react-input-mask:

    Simple Phone Number Masking with react-input-mask

    import React from 'react';
    import InputMask from 'react-input-mask';
    
    const PhoneNumberInput = () => {
      return (
        <InputMask mask="(999) 999-9999" placeholder="Enter phone number">
          {(inputProps) => <input {...inputProps} />}
        </InputMask>
      );
    };
    
    export default PhoneNumberInput;
    
  • react-text-mask:

    Custom Masking with react-text-mask

    import React from 'react';
    import { TextMask } from 'react-text-mask';
    
    const CustomMaskedInput = () => {
      return (
        <TextMask
          mask={(value) => {
            const mask = []; // Define your custom mask logic here
            return mask;
          }}
          placeholder="Enter custom masked input"
        />
      );
    };
    
    export default CustomMaskedInput;
    
How to Choose: react-number-format vs react-phone-number-input vs react-input-mask vs react-text-mask
  • react-number-format:

    Select react-number-format if you require advanced number formatting features, such as currency, percentages, and custom number formats. It provides better control over numeric inputs and is ideal for applications that handle financial data.

  • react-phone-number-input:

    Opt for react-phone-number-input if your primary focus is on phone number input. It provides international phone number formatting and validation, making it a great choice for applications that need to handle global phone number formats.

  • react-input-mask:

    Choose react-input-mask if you need a simple and flexible solution for creating input masks. It allows you to define custom masks easily and is lightweight, making it suitable for projects where performance is a concern.

  • react-text-mask:

    Use react-text-mask if you want a highly customizable masking solution that works with any type of input. It allows for more complex masking scenarios and is suitable for projects that require detailed control over how input is displayed and validated.

README for react-number-format

Actions Status

react-number-format

React Number Format is an input-formatter library with a sophisticated and light weight caret engine. It ensures that a user can only enter text that meets specific numeric or string patterns, and formats the input value for display.

Features

  1. Prefix, suffix and thousands separator.
  2. Input Masking.
  3. Format number in an input or format as a simple text.
  4. Custom pattern formatting.
  5. Custom formatting handler.
  6. Fully customizable

Demos

See the many DEMO sections in the documentation.

Install

npm

Using npm

npm install react-number-format

Using yarn

yarn add react-number-format

Documentation

Read the full documentation here https://s-yadav.github.io/react-number-format/docs/intro

ES6

Numeric Format

import { NumericFormat } from 'react-number-format';

NumericFormat Props: https://s-yadav.github.io/react-number-format/docs/numeric_format

Pattern Format

import { PatternFormat } from 'react-number-format';

PatternFormat Props: https://s-yadav.github.io/react-number-format/docs/pattern_format

Migrate from v4 to v5

https://s-yadav.github.io/react-number-format/docs/migration

v4 doc

v4 Docs

Development

  • Clone the repository or download the zip
  • npm i -g yarn to download Yarn
  • yarn to install dependencies
  • yarn start to run example server (http://localhost:8084/)
  • yarn test to test changes
  • yarn build to bundle files

Testing

Test cases are written in jasmine and run by karma

Test files : /test/**/*.spec.js

To run test : yarn test