Usage
MaskInput is an input with a fixed format, for example a phone number or a credit card.
The mask string defines the format, see mask pattern syntax.
The component is built on top of the useMask hook:
Input description
MaskInput with uncontrolled forms
Set name to submit the displayed text, including mask characters. defaultValue takes the raw value:
Dynamic mask
Use the modify option to change the mask based on the current value, for example
to switch between standard and American Express credit card formats:
Custom tokens
Override or extend the built-in token map with the tokens option:
Regex array format
When built-in tokens are not enough, pass an array of string literals and RegExp objects:
Transform
Use the transform option to convert each character before validation, for example
to uppercase letters so that the A token accepts lowercase input:
Type lowercase letters – they will be auto-uppercased
Disabled state
Error state
Invalid phone number
Success state
Looks good!
Controlled
value, defaultValue and onChange work with the raw value without the mask, for example
1234567890 for (123) 456-7890. Use onChangeRaw to get both raw and masked values:
Reset value
To clear a controlled input, set value to an empty string.
In uncontrolled mode, use the resetRef prop to get a function that clears the input:
With use-form
MaskInput works with use-form like any other input. The form receives
the raw value without the mask, for example 1234567890 for (123) 456-7890.
Values passed to initialValues, form.setFieldValue and form.reset must be raw values too:
Mask pattern syntax
Each character of the mask string is either a token, an editable slot, or a literal, a fixed character inserted automatically.
Built-in tokens
9– any single digit ([0-9])a– any single letter ([A-Za-z])A– any uppercase letter ([A-Z])*– any alphanumeric character ([A-Za-z0-9])#– digit or sign ([-+0-9])
Optional segments
Append ? after the last required character to mark remaining slots as optional.
When the input is not focused, the empty optional part is hidden.
With alwaysShowMask, it stays visible with slot characters:
Escaping
Prefix a token character with \ to treat it as a literal: