KeyFilterKeyFilter directive restricts user input based on a regular expression.

KeyFilter - 图1

Documentation” class=”ng-tns-c2-1

Import

  1. import {KeyFilterModule} from 'primeng/keyfilter';
  2.  

Getting Started

KeyFilter directive is applied to an input text element using pKeyFilter attribute whose value is either a built-in regular expression name or a custom one. Following input only accepts integers.

  1. <input type="text" pKeyFilter="int">
  2.  

Built-in Filters

Commonly used cases have their own built-in shortcuts.

  • pint: Positive integers
  • int: Integers
  • pnum: Positive numbers
  • num: Numbers
  • hex: Hexadecimal
  • email: Email
  • alpha: Alphabetic
  • alphanum: Alphanumeric

Custom Filter

A custom filter is provided by binding a regular expression, here is an example that blocks special characters including "< > * !"

  1. <input type="text" [pKeyFilter]="noSpecial">
  2.  
  1. export class KeyFilterDemo {
  2. noSpecial: RegExp = /^[^<>*!]+$/
  3. }
  4.  

Validate Mode

Instead of blocking a single keypress, the alternative validation mode which is enabled with pValidateOnly property validates the whole input with a built-in Angular validator.

  1. <form #form="ngForm">
  2. <label for="cc">Credit Card</label>
  3. <input id="cc" type="text" name="cc" [(ngModel)]="cc" pInputText [pKeyFilter]="ccRegex" [pValidateOnly]="true" placeholder="1234-1234-1234-1234">
  4. <p-message severity="error" text="Not a valid number" [@errorState]="form.dirty && !form.valid ? 'visible' : 'hidden'"></p-message>
  5. </form>
  6.  

Properties

NameTypeDefaultDescription
pValidateOnlybooleanfalseWhen enabled, instead of blocking keys, input is validated internally to test against the regular expression.

Styling

This directive does not apply any styling.

Dependencies

None.

Source” class=”ng-tns-c2-1

View on GitHub

  1. <h3>Filtering</h3>
  2. <div class="ui-g ui-fluid">
  3. <div class="ui-g-12 ui-md-4">
  4. <input type="text" pInputText pKeyFilter="int" placeholder="Integers">
  5. </div>
  6. <div class="ui-g-12 ui-md-4">
  7. <input type="text" pInputText pKeyFilter="num" placeholder="Numbers">
  8. </div>
  9. <div class="ui-g-12 ui-md-4">
  10. <input type="text" pInputText pKeyFilter="money" placeholder="Money">
  11. </div>
  12. <div class="ui-g-12 ui-md-4">
  13. <input type="text" pInputText pKeyFilter="hex" placeholder="Hex">
  14. </div>
  15. <div class="ui-g-12 ui-md-4">
  16. <input type="text" pInputText pKeyFilter="alpha" placeholder="Alphabetic">
  17. </div>
  18. <div class="ui-g-12 ui-md-4">
  19. <input type="text" pInputText pKeyFilter="alphanum" placeholder="Alphanumberic">
  20. </div>
  21. <div class="ui-g-12 ui-md-4">
  22. <input type="text" pInputText [pKeyFilter]="blockSpecial" placeholder="Block # < > * !">
  23. </div>
  24. <div class="ui-g-12 ui-md-4">
  25. <input type="text" pInputText [pKeyFilter]="blockSpace" placeholder="Block space key">
  26. </div>
  27. </div>
  28. <h3>Validation Mode</h3>
  29. <form #form="ngForm">
  30. <label for="cc" style="display:block;margin-bottom:4px">Credit Card</label>
  31. <input id="cc" type="text" name="cc" [(ngModel)]="cc" pInputText [pKeyFilter]="ccRegex" [pValidateOnly]="true" placeholder="1234-1234-1234-1234" style="margin-right: .5em">
  32. <p-message severity="error" text="Not a valid number" [@errorState]="form.dirty && !form.valid ? 'visible' : 'hidden'"></p-message>
  33. </form>
  34.  
  1. export class KeyFilterDemo {
  2. blockSpecial: RegExp = /^[^<>*!]+$/
  3. blockSpace: RegExp = /[^\s]/;
  4. ccRegex: RegExp = /[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/;
  5. cc: string;
  6. }
  7.