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

Mask - 图1

Documentation

Import

  1. import {InputMaskModule} from 'primeng/inputmask';
  2.  

Getting Started

Component is defined using p-inputMask element with a mask and two-way value binding is enabled with standard ngModel directive.

  1. <p-inputMask [(ngModel)]="val" mask="99-9999"></p-inputMask>
  2.  

Mask

Mask format can be a combination of the the following built-in definitions.

  • a - Alpha character (defaut: A-Z,a-z)
  • 9 - Numeric character (0-9)
      • Alpha numberic character (A-Z,a-z,0-9)
  1. <p-inputMask [(ngModel)]="val" mask="a*-999-a999"></p-inputMask>
  2.  

You can also define your own regex pattern for alpha character

  1. <p-inputMask [(ngModel)]="val" mask="99-aa" characterPattern="[А-Яа-я]"></p-inputMask>
  2.  

SlotChar

Underscore is the default placeholder for a mask and this can be customized using slotChart option.

  1. <p-inputMask [(ngModel)]="val" mask="99-9999" slotChar=" "></p-inputMask>
  2.  

Optional Values

If the input does not complete the mask definition, it is cleared by default. Use autoClear property to control this behavior. In addition, certain part of a mask can be made optional by using ? symbol where anything after the question mark becomes optional.

Model Driven Forms

InputMask can be used in a model driven form as well.

  1. <p-inputMask formControlName="username" mask="(999) 999-9999? x99999"></p-inputMask>
  2.  

Properties

NameTypeDefaultDescription
typestringtextHTML5 input type
maskstringnullMask pattern.
slotCharstring_Placeholder character in mask, default is underscore.
autoClearbooleantrueClears the incomplete value on blur.
unmaskbooleanfalseDefines if ngModel sets the raw unmasked value to bound value or the formatted mask value.
stylestringnullInline style of the input field.
styleClassstringnullStyle class of the input field.
placeholderstringnullAdvisory information to display on input.
sizenumbernullSize of the input field.
maxlengthnumbernullMaximum number of character allows in the input field.
tabindexnumbernullSpecifies tab order of the element.
disabledbooleanfalseWhen present, it specifies that the element value cannot be altered.
readonlybooleanfalseWhen present, it specifies that an input field is read-only.
namestringnullName of the input field.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
requiredbooleanfalseWhen present, it specifies that an input field must be filled out before submitting the form.
characterPatternstring[A-Za-z]Regex pattern for alpha characters
autoFocusbooleanfalseWhen present, the input gets a focus automatically on load.
autocompletestringnullUsed to define a string that autocomplete attribute the current element.
ariaLabelstringnullUsed to define a string that labels the current element.
ariaRequiredbooleanfalseUsed to indicate that user input is required on an element before a form can be submitted.

Events

NameParametersDescription
onFocusevent: Browser eventCallback to invoke when input receives focus.
onBlurevent: Browser eventCallback to invoke when input loses focus.
onComplete-Callback to invoke on when user completes the mask pattern.
onInput-Callback to invoke on when the input field value is altered.

Methods

NameParametersDescription
focus-Applies focus to the input.

Styling

Styling is same as inputtext component, for theming classes visit theming page.

Dependencies

None.

Source

View on GitHub

  1. <div class="ui-g ui-fluid">
  2. <div class="ui-g-12 ui-md-6 ui-lg-4">
  3. <span>Basic</span>
  4. <p-inputMask mask="99-999999" [(ngModel)]="val1" placeholder="99-999999"></p-inputMask>
  5. </div>
  6. <div class="ui-g-12 ui-md-6 ui-lg-4">
  7. <span>SSN</span>
  8. <p-inputMask mask="999-99-9999" [(ngModel)]="val2" placeholder="999-99-9999"></p-inputMask>
  9. </div>
  10. <div class="ui-g-12 ui-md-6 ui-lg-4">
  11. <span>Date</span>
  12. <p-inputMask mask="99/99/9999" [(ngModel)]="val3" placeholder="99/99/9999" slotChar="mm/dd/yyyy"></p-inputMask>
  13. </div>
  14. <div class="ui-g-12 ui-md-6 ui-lg-4">
  15. <span>Phone</span>
  16. <p-inputMask mask="(999) 999-9999" [(ngModel)]="val4" placeholder="(999) 999-9999"></p-inputMask>
  17. </div>
  18. <div class="ui-g-12 ui-md-6 ui-lg-4">
  19. <span>Phone Ext</span>
  20. <p-inputMask mask="(999) 999-9999? x99999" [(ngModel)]="val5" placeholder="(999) 999-9999? x99999"></p-inputMask>
  21. </div>
  22. <div class="ui-g-12 ui-md-6 ui-lg-4">
  23. <span>Serial Number</span>
  24. <p-inputMask mask="a*-999-a999" [(ngModel)]="val6" placeholder="a*-999-a999"></p-inputMask>
  25. </div>
  26. </div>
  27.  
  1. export class InputMaskDemo {
  2. val1: string;
  3. val2: string;
  4. val3: string;
  5. val4: string;
  6. val5: string;
  7. val6: string;
  8. }
  9.