React Number Format

5.4.5 · active · verified Sun Apr 19

react-number-format is a React component library designed for formatting numeric and pattern-based input fields, as well as displaying formatted text. It provides functionalities for adding prefixes, suffixes, and thousands separators, alongside comprehensive input masking capabilities for various patterns like credit card numbers or phone numbers. The library is currently stable at version 5.4.5 and is actively maintained with frequent bug fix releases, supporting a wide range of React versions up to 19. Its key differentiators include a sophisticated caret engine that handles complex formatting scenarios, custom pattern formatting, and the ability to define custom formatting handlers, offering a high degree of customization for user input experiences. The project emphasizes robust handling of user input while ensuring display consistency.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `NumericFormat` for currency input with thousands separators and decimal precision, and `PatternFormat` for a phone number mask, showing how to manage their values with React state.

import React, { useState } from 'react';
import { NumericFormat, PatternFormat } from 'react-number-format';

function MyFormattedInputs() {
  const [numericValue, setNumericValue] = useState('');
  const [phoneValue, setPhoneValue] = useState('');

  return (
    <div>
      <h2>Numeric Input (Currency)</h2>
      <NumericFormat
        value={numericValue}
        onValueChange={(values) => {
          setNumericValue(values.value);
        }}
        thousandSeparator={true}
        prefix={'$'}
        decimalScale={2}
        fixedDecimalScale
        placeholder="Enter amount"
      />

      <h2>Phone Number Input</h2>
      <PatternFormat
        value={phoneValue}
        onValueChange={(values) => {
          setPhoneValue(values.value);
        }}
        format="(###) ###-####"
        mask="_"
        placeholder="(123) 456-7890"
      />

      <p>Current Numeric Value: {numericValue}</p>
      <p>Current Phone Value: {phoneValue}</p>
    </div>
  );
}

export default MyFormattedInputs;

view raw JSON →