Styleable, Accessble Input Masking
npm install input-maskingThis is a fork of Estelle Wyel's input-masking adapted specifically for use in React projects.
Where possible, I maintained the spirit of the original project - particularly the intent to have styleable, accessible input forms.
input-mask enables you to include a mask on any input where a specific data entry format is required. The placeholder text remains in place, displaying which characters still need to be included. The placeholder is CSS styleable.
The user can enter letters and numbers. All other characters, like spaces, dashes, and parenthesis are automatically added by the script, making data entery easier when using dynamic keypads.
Install input-masking in your project:npm i input-masking
To use:
``javascript
import { MaskedInput } from "input-masking";
type="tel"
placeholder="MM/YY"
pattern="(1[0-2]|0[1-9])\/(1[5-9]|2\d)"
data-valid-example="05/18"
label="Credit Card Expiration"
/>;
`
There are accessibility features baked into the examples that you must maintain to maintain accessibility.
- Always include a label for each form control, and associate the form control either implicitly by nesting it, or explicitly with the htmlFfor and id attributes.title
- Always include a attribute that describes the expected pattern when including the pattern attribute.type
- Always use the best input for the job, so dynamic touchpad users get the right touchpad for the job. Generally this will always be type="tel", as most masking is for digits only. However, when alphanumeric characters are required, use type="text". And, while I've included an expiration month to show an example of using complex regular expressions, use type="month" instead of this script.
If you are ok with all the default options you can have masked-inputs initalize it self and avoid writing any JavaScript at all. Simplu add the attribute data-autoinit="false" to your script tag
`html``
var React = require('react'), MaskedInput = require('../index'); window.onload =
function () { React.render(
type="tel"
placeholder="MM/YY"
pattern="(1[0-2]|0[1-9])\/\d\d"
data-valid-example="11/18"
title="2-digit month and 2-digit year greater than 01/15"
/>
type="tel"
placeholder="XXXXX"
pattern="\d{5}"
required
title="5-digit zip code"
/>
, document.getElementById('component') ); };
This code is available under the MIT license
Thanks to @stevefaulkner