InputMask component is used to enter input in a certain format such as numeric, date, currency, email and phone.
import InputMask from 'primevue/inputmask';
InputMask is used as a controlled input with v-model properties.
<InputMask id="basic" v-model="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.
<div class="flex-auto">
<label for="ssn" class="font-bold block mb-2">SSN</label>
<InputMask id="ssn" v-model="value1" mask="999-99-9999" placeholder="999-99-9999" />
</div>
<div class="flex-auto">
<label for="phone" class="font-bold block mb-2">Phone</label>
<InputMask id="phone" v-model="value2" mask="(999) 999-9999" placeholder="(999) 999-9999" />
</div>
<div class="flex-auto">
<label for="serial" class="font-bold block mb-2">Serial</label>
<InputMask id="serial" v-model="value3" mask="a*-999-a999" placeholder="a*-999-a999" />
</div>
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 v-model="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 id="basic" v-model="value" placeholder="99/99/9999" mask="99/99/9999" slotChar="mm/dd/yyyy" />
A floating label appears on top of the input field when focused.
<span class="p-float-label">
<InputMask id="ssn" v-model="value" mask="999-99-9999" placeholder="999-99-9999" />
<label for="ssn">SSN</label>
</span>
Invalid state style is added using the p-invalid class to indicate a failed validation.
<InputMask v-model="value" mask="99-999999" placeholder="99-999999" class="p-invalid"/>
When disabled is present, the element cannot be edited and focused.
<InputMask mask="99-999999" placeholder="99-999999" disabled />
Compatibility with popular Vue form libraries.
VeeValidate is a popular library for handling forms in Vue.
<template>
<div class="card flex justify-content-center">
<form @submit="onSubmit" class="flex flex-column gap-2">
<InputMask v-model="value" mask="(999) 999-9999" placeholder="(999) 999-9999"
:class="{ 'p-invalid': errorMessage }" aria-describedby="mask-error" />
<small class="p-error" id="mask-error">{{ errorMessage || ' ' }}</small>
<Button type="submit" label="Submit" />
</form>
<Toast />
</div>
</template>
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 for="date">Date</label>
<InputMask id="date" />
<span id="phone">Phone</span>
<InputMask aria-labelledby="phone" />
<InputMask aria-label="Age" />
Key | Function |
---|---|
tab | Moves focus to the input. |