InputMask

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


import { InputMask } from 'primeng/inputmask';

InputMask is used as a controlled input with ngModel properties.


<p-inputmask mask="99-999999" [(ngModel)]="value" placeholder="99-999999" />

InputMask can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.


<p-inputmask mask="99-999999" formControlName="value" 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.

SSN
Phone
Serial Number

<span class="font-bold block mb-2">SSN</span>
<p-inputmask mask="999-99-9999" [(ngModel)]="value1" placeholder="999-99-9999" />
<span class="font-bold block mb-2">Phone</span>
<p-inputmask mask="(999) 999-9999" [(ngModel)]="value2" placeholder="(999) 999-9999" />
<span class="font-bold block mb-2">Serial Number</span>
<p-inputmask mask="a*-999-a999" [(ngModel)]="value3" placeholder="a*-999-a999" />

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.


<p-inputmask mask="(999) 999-9999? x99999" [(ngModel)]="value" placeholder="(999) 999-9999? x99999" />

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


<p-inputmask [(ngModel)]="value" mask="99/99/9999" placeholder="99/99/9999" slotChar="mm/dd/yyyy" />

Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.


<p-inputmask mask="99-999999" [(ngModel)]="value" variant="filled" placeholder="99-999999" />

FloatLabel visually integrates a label with its form element. Visit FloatLabel documentation for more information.


<p-floatlabel>
    <p-inputmask id="over_label" [(ngModel)]="value1" mask="999-99-9999" />
    <label for="over_label">Over Label</label>
</p-floatlabel>

<p-floatlabel variant="in">
    <p-inputmask id="in_label" [(ngModel)]="value2" mask="999-99-9999" />
    <label for="in_label">In Label</label>
</p-floatlabel>

<p-floatlabel variant="on">
    <p-inputmask id="on_label" [(ngModel)]="value3" mask="999-99-9999" />
    <label for="on_label">On Label</label>
</p-floatlabel>

IftaLabel is used to create infield top aligned labels. Visit IftaLabel documentation for more information.


<p-iftalabel>
    <p-inputmask id="ssn" [(ngModel)]="value" mask="999-99-9999" autocomplete="off" />
    <label for="ssn">SSN</label>
</p-iftalabel>

InputMask provides small and large sizes as alternatives to the base.


<p-inputmask [(ngModel)]="value1" placeholder="Small" size="small" mask="99-999999" />
<p-inputmask [(ngModel)]="value2" placeholder="Normal" mask="99-999999" />
<p-inputmask [(ngModel)]="value3" placeholder="Large" size="large" mask="99-999999" />

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


<p-inputmask mask="999-99-9999" [(ngModel)]="value" class="ng-invalid ng-dirty" placeholder="Serial Key" />

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


<p-inputmask mask="999-99-9999" [(ngModel)]="value" [disabled]="true" />

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 ariaLabelledBy, ariaLabel props.


<label for="date">Date</label>
<p-inputmask inputId="date"/>

<span id="phone">Phone</span>
<p-inputmask ariaLabelledBy="phone"/>

<p-inputmask ariaLabel="Age"/>

Keyboard Support

KeyFunction
tabMoves focus to the input.