Open In App

Angular PrimeNG Form KeyFilter Built-in Filters Component

Last Updated : 05 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • pint: It is used for the positive integers built-in shortcut.
  • int: It is used for the integers built-in shortcut.
  • pnum: It is used for positive numbers built-in shortcuts.
  • num: It is used for numbers built-in shortcuts.
  • hex: It is used for the hexadecimal built-in shortcuts.
  • email: It is used for the email built-in shortcut.
  • alpha: It is used for the alphabetic built-in shortcut.
  • alphanum: It is used for the alphanumeric built-in shortcut.

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:

 

  • Steps to run the application: Run the below command to see the output:
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.

  • app.component.html:

HTML




<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">


  • app.component.ts:

Javascript




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


  • app.module.ts:

Javascript




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.

  • app.component.html:

HTML




<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">


  • app.component.ts:

Javascript




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


  • app.module.ts:

Javascript




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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads