React Masked Input Component

4.0.1 · abandoned · verified Sun Apr 19

react-maskedinput is a React component designed to facilitate masked input fields, enabling developers to enforce specific input patterns for data like phone numbers, dates, or credit card numbers. It is built on top of the `inputmask-core` library for its core masking logic. The current stable version is 4.0.1, released in January 2018. Due to its age and the last release date, the project's release cadence is irregular and it appears to be largely unmaintained. Key features include support for custom format characters and controlled component behavior through the `value` prop. A significant note from the author indicates, "This project has never been used by its author, other than while making it," which might suggest limited real-world testing or ongoing support. Developers should consider its age and potential compatibility issues with modern React versions and ecosystems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `MaskedInput` components into a React class component for various masked inputs like credit card numbers, expiry dates, and CCVs. It also shows how to define custom mask characters.

import React from 'react';
import MaskedInput from 'react-maskedinput';

class CreditCardDetails extends React.Component {
  state = {
    card: '',
    expiry: '',
    ccv: ''
  };

  _onChange = (e) => {
    this.setState({ [e.target.name]: e.target.value });
  };

  render() {
    return (
      <div className="CreditCardDetails">
        <label>
          Card Number:{' '}
          <MaskedInput mask="1111 1111 1111 1111" name="card" size="20" onChange={this._onChange} />
        </label>
        <label>
          Expiry Date:{' '}
          <MaskedInput mask="11/1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange} />
        </label>
        <label>
          CCV:{' '}
          <MaskedInput mask="111" name="ccv" onChange={this._onChange} />
        </label>
      </div>
    );
  }
}

// Example of a custom masked input wrapper
function CustomAlphaNumericInput(props) {
  return (
    <MaskedInput
      mask="AAAA-1111"
      placeholder="ABCD-1234"
      size="10"
      {...props}
      formatCharacters={{
        'A': {
          validate(char) { return /[a-zA-Z]/.test(char) },
          transform(char) { return char.toUpperCase() }
        }
      }}
    />
  );
}

export default CreditCardDetails;

view raw JSON →