KeyFilter is a built-in feature of InputText to restrict user input based on a regular expression.
import { KeyFilterModule } from 'primeng/keyfilter';
KeyFilter provides various presets configured with the pKeyFilter property.
<input pInputText pKeyFilter="int" />
<input pInputText pKeyFilter="num" />
<input pInputText pKeyFilter="money" />
<input pInputText pKeyFilter="hex" />
<input pInputText pKeyFilter="alpha" />
<input pInputText pKeyFilter="alphanum" />
In addition to the presets, a regular expression can be configured for customization.
<input pInputText [pKeyFilter]="blockSpace" />
<input pInputText [pKeyFilter]="blockChars" />
KeyFilter can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
<form [formGroup]="formGroup">
<label for="integer" class="font-bold block mb-2"> Integer </label>
<input pInputText id="integer" pKeyFilter="int" class="w-full" formControlName="value"/>
</form>
API defines helper props, events and others for the PrimeNG KeyFilter module.
Name | Type | Default | Description |
---|---|---|---|
pValidateOnly | boolean | false | When enabled, instead of blocking keys, input is validated internally to test against the regular expression. |