Open In App

Angular PrimeNG Form KeyFilter Built-in Filters Component

Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see how to use the KeyFilter Built-in Filters Component in Angular PrimeNG.

The KeyFilter component is used to set the data type that is to be used for an input field. It also filters other data types from the entered data types. Built-in Filters are used to filter the input according to the value provided for the pKeyFilter attribute. There are 8 built-in filters: pint, int, pnum, num, hex, email, alpha, alphanum, which are described below.



Angular PrimeNG Form KeyFilter Built-in Filters:

Syntax:



<input type="text" [pKeyFilter]="...">

Creating Angular Application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

ng serve --save

Example 1: This basic example shows how to use the Angular PrimeNG Form KeyFilter Built-in Filters Component using the pint, int, pnum, and num built-in filters.




<h1 style="color: green">
  GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter 
      Built-in Filters Component
</h5>
  
<input pInputText type="text" 
       pKeyFilter="pint" 
       placeholder="Positive Integers Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="int" 
       placeholder="Only Integers are Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="pnum" 
       placeholder="Positive Numbers Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="num" 
       placeholder="Only Numbers are Allowed">




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent { }




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { InputTextModule } from 'primeng/inputtext';
import { MessageModule } from 'primeng/message';
import { KeyFilterModule } from 'primeng/keyfilter';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        KeyFilterModule,
        InputTextModule,
        MessageModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

Output:

 

Example 2: This is another basic example that shows how to use the Angular PrimeNG Form KeyFilter Built-in Filters Component using the hex, email, alpha, and alphanum built-in filters.




<h1 style="color: green">
  GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter 
    Built-in Filters Component
</h5>
  
<input pInputText type="text" 
       pKeyFilter="hex" 
       placeholder="Hexadecimal Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="email" 
       placeholder="Email Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="alpha" 
       placeholder="Alphabets Allowed">
<br><br>
<input pInputText type="text" 
       pKeyFilter="alphanum" 
       placeholder="Alphanumeric Allowed">




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent { }




import { NgModule } from '@angular/core';
import { BrowserModule }
    from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { InputTextModule } from 'primeng/inputtext';
import { MessageModule } from 'primeng/message';
import { KeyFilterModule } from 'primeng/keyfilter';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        KeyFilterModule,
        InputTextModule,
        MessageModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

Output:

 

Reference: https://primefaces.org/primeng/keyfilter


Article Tags :