Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-maskedinput

insin212.5kMIT4.0.1TypeScript support: definitely-typed

Masked <input/> React component

react, masked, input, react-component

readme

MaskedInput

A React component for <input> masking, built on top of inputmask-core.

This project has never been used by its author, other than while making it.

Live Demo

Install

npm

npm install react-maskedinput --save

Browser bundle

The browser bundle exposes a global MaskedInput variable and expects to find a global React variable to work with.

Usage

Give MaskedInput a mask and an onChange callback:

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>
  }
}

Create some wrapper components if you have a masking configuration which will be reused:

function CustomInput(props) {
  return <MaskedInput
    mask="1111-WW-11"
    placeholder="1234-WW-12"
    size="11"
    {...props}
    formatCharacters={{
      'W': {
        validate(char) { return /\w/.test(char ) },
        transform(char) { return char.toUpperCase() }
      }
    }}
  />
}

Props

mask : string

The masking pattern to be applied to the <input>.

See the inputmask-core docs for supported formatting characters.

onChange : (event: SyntheticEvent) => any

A callback which will be called any time the mask's value changes.

This will be passed a SyntheticEvent with the input accessible via event.target as usual.

Note: this component currently calls onChange directly, it does not generate an onChange event which will bubble up like a regular <input> component, so you must pass an onChange if you want to get a value back out.

formatCharacters: Object

Customised format character definitions for use in the pattern.

See the inputmask-core docs for details of the structure of this object.

placeholderChar: string

Customised placeholder character used to fill in editable parts of the pattern.

See the inputmask-core docs for details.

value : string

A default value for the mask.

placeholder : string

A default placeholder will be generated from the mask's pattern, but you can pass a placeholder prop to provide your own.

size : number | string

By default, the rendered <input>'s size will be the length of the pattern, but you can pass a size prop to override this.

Other props

Any other props passed in will be passed as props to the rendered <input>, except for the following, which are managed by the component:

  • maxLength - will always be equal to the pattern's .length
  • onKeyDown, onKeyPress & onPaste - will each trigger a call to onChange when necessary

MIT Licensed

changelog

4.0.1 / 2018-01-26 🇦🇺

4.0.0 / 2017-07-04

  • Potential breaking change as the peerDependencies range has been changed from "0.14.x || 15.x" to "^0.14.9 || ^15.3.0".

  • Use React.Component instead of React.createClass and the prop-types package instead of React.PropTypes to silence deprecation warnings [#94] [krvital]

  • Update nwb to 0.17.x:

    • module config replaces jsnext:main in package.json to specify the location of the ES6 modules build.
    • prop-types is bundled with the UMD development build and stripped from the production build, along with usage of propTypes.

3.3.4 / 2016-12-15

3.3.2 / 2016-12-01

  • Fix for both Android and MS Edge input entering

3.2.0 / 2016-05-24

3.1.3 / 2016-05-02

  • Don’t call onChange function if undefined.
  • Update nwb to 0.9.x

3.1.2 / 2016-04-11

  • Support for React 15.x.x

3.1.1 / 2016-03-09

  • Convert tooling to use nwb [[bpugh]][[bpugh]]
  • Publish dist files

3.1.0 / 2016-02-11

  • Added support for value behaving as a controlled component.

3.0.0 / 2015-10-23

Breaking change: Now uses a mask prop to define the input mask instead of pattern, to avoid preventing use of the the HTML5 pattern attribute in conjunction with the input mask.

Breaking change: React >= 0.14 is now required.

React 0.14 compatibility. [jquense]

Updated to inputmask-core@2.1.1

Updates based on inputmask-core@2.1.0:

  • Added placeholderChar prop to configure the placeholder character.
  • The mask's pattern is now changed if the pattern prop changes - the user's input so far is replayed with the new pattern (with mixed results - TBD).

UMD build is now available via npm in dist/. [muffinresearch]

2.0.0 / 2015-04-07

Breaking change: inputmask-core@2.0.0 is now required.

Added undo/redo when Ctrl/Command + Z/Y are used.

1.1.0 / 2015-03-26

Updated to inputmask-core@1.2.0

A formatCharacters prop can now be passed to configure input mask format characters.

1.0.0 / 2015-03-25

Initial release features:

  • Based on inputmask-core@1.1.0
  • Basic editing works:
    • Typing, backspacing, pasting, cutting and deleting
    • Invalid content will be ignored if typed or pasted
    • Static parts of the mask can't be modfied
    • Editing operations can handle text selections
    • Tested in latest versions of Firefox, Chrome, Opera and IE
  • Placeholder generation and display when the mask has no user input