InputMask

InputMask component is used to enter input in a certain format such as numeric, date, currency, email and phone.


import { InputMask } from 'primereact/inputmask';
         

InputMask is used as a controlled input with value and onChange properties along with the mask property to define the mask.


<InputMask value={value} onChange={(e) => setValue(e.target.value)} mask="99-999999" placeholder="99-999999" />
         

Mask format can be a combination of the following definitions; a for alphabetic characters, 9 for numeric characters and * for alphanumberic characters. In addition, formatting characters like ( , ) , - are also accepted.


<label htmlFor="ssn" className="font-bold block mb-2">SSN</label>
<InputMask id="ssn" mask="999-99-9999" placeholder="999-99-9999"></InputMask>

<label htmlFor="phone" className="font-bold block mb-2">Phone</label>
<InputMask id="phone" mask="(999) 999-9999" placeholder="(999) 999-9999"></InputMask>

<label htmlFor="serial" className="font-bold block mb-2">Serial</label>
<InputMask id="serial" mask="a*-999-a999" placeholder="a*-999-a999"></InputMask>
         

When the input does not complete the mask definition, it is cleared by default. Use autoClear property to control this behavior. In addition, ? is used to mark anything after the question mark optional.


<InputMask value={value} onChange={(e) => setValue(e.target.value)} mask="(999) 999-9999? x99999" placeholder="(999) 999-9999? x99999"  />
         

Default placeholder for a mask is underscore that can be customized using slotChar property.


<InputMask value={value} onChange={(e) => setValue(e.target.value)} mask="99/99/9999" placeholder="99/99/9999" slotChar="mm/dd/yyyy" />
         

A floating label appears on top of the input field when focused.


<span className="p-float-label">
    <InputMask id="ssn_input" value={value} onChange={(e) => setValue(e.target.value)} mask="999-99-9999" />
    <label htmlFor="ssn_input">SSN</label>
</span>
         

Invalid state style is added using the p-invalid class to indicate a failed validation.


<InputMask mask="99-999999" placeholder="99-999999" className="p-invalid"/>
         

When disabled is present, the element cannot be edited and focused.


<InputMask mask="99-999999" placeholder="99-999999" disabled />
         

Compatibility with popular React form libraries.

Formik is a popular library for handling forms in React.

 

<Toast ref={toast} />
<InputMask
    id="value"
    name="value"
    value={formik.values.value}
    onChange={(e) => {
        formik.setFieldValue('value', e.target.value);
    }}
    mask="99-999999"
    placeholder="99-999999"
    className={classNames({ 'p-invalid': isFormFieldInvalid('value') })}
/>
{getFormErrorMessage('value')}
<Button type="submit" label="Submit" />
 

React Hook Form is another popular React library to handle forms.

 

<Toast ref={toast} />
<Controller
    name="value"
    control={control}
    rules={{ required: 'Phone is required.' }}
    render={({ field, fieldState }) => (
        <>
            <label htmlFor={field.name} className={classNames({ 'p-error': errors.value })}></label>
            <InputMask
                id={field.name}
                value={field.value}
                className={classNames({ 'p-invalid': fieldState.error })}
                onChange={(e) => field.onChange(e.target.value)}
                mask="99-999999"
                placeholder="99-999999"
            />
            {getFormErrorMessage(field.name)}
        </>
    )}
/>
<Button label="Submit" type="submit" icon="pi pi-check"/>      
 

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-inputtextInput element
p-inputmaskInput element
Accessibility guide documents the specification of this component based on WCAG guidelines, the implementation is in progress.

Screen Reader

InputMask component renders a native input element that implicitly includes any passed prop. Value to describe the component can either be provided via label tag combined with id prop or using aria-labelledby, aria-label props.


<label htmlFor="date">Date</label>
<InputMask id="date" />

<span id="phone">Phone</span>
<InputMask aria-labelledby="phone" />

<InputMask aria-label="Age" />
     

Keyboard Support

KeyFunction
tabMoves focus to the input.