Open In App

Angular PrimeNG Form KeyFilter Custom Filter 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 Form KeyFilter Custom Filter 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. The Custom Filter can be used to bind the regular expression with the help of the pKeyFilter property.

Syntax:

// In HTML file
<input type="text" [pKeyFilter]="...">

// Typescript file
export class GFGDemo {
    noSpecial: RegExp = /^[^<>*!]+$/
}

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:

 

  • To run the above file run the below command:
ng serve --save

Example 1: This basic example shows how to use the Angular PrimeNG Form KeyFilter Custom Filter Component. Here “(, ), @” is not allowed.

  • app.component.html:

HTML




<h1 style="color: green">
  GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter 
      Custom Filter Component
</h5>
  
<form #form="ngForm">
    <input name="gfg" 
            [(ngModel)]="gfg" 
            [pValidateOnly]="true" 
            type="text" 
            pInputText 
            [pKeyFilter]="specialChar" 
            placeholder="()@ not allowed" 
            style="margin-right: .5em">
    <p-message severity="error" 
               text="Invalid character" 
               [@errorState]=
          "form.dirty && !form.valid ? 'show' : 'hide'">
    </p-message>
</form>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { trigger, state, style } 
    from '@angular/animations';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    animations: [
        trigger('errorState', [
            state('hide', style({
                opacity: 0
            })),
  
            state('show', style({
                opacity: 1
            }))
        ])
    ],
})
  
export class AppComponent {
    specialChar: RegExp = /^[^()@]+$/
    gfg: string;
}


  • 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: Below is another basic example that shows how to use the Angular PrimeNG Form KeyFilter Custom Filter Component. Here, we are validating the email.

  • app.component.html:

HTML




<h1 style="color: green">
      GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter
      Custom Filter Component
</h5>
  
<form #form="ngForm">
    <input name="gfg" 
            [(ngModel)]="gfg" 
            [pValidateOnly]="true" 
            type="text" 
            pInputText 
            [pKeyFilter]="emailValidate" 
            placeholder="Ex- abc111@gmail.com" 
            style="margin-right: .5em">
  
    <p-message severity="error" 
                text="Invalid Email" 
                [@errorState]=
           "form.dirty && !form.valid ? 'show' : 'hide'">
    </p-message>
</form>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
import { trigger, state, style } 
    from '@angular/animations';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    animations: [
        trigger('errorState', [
            state('hide', style({
                opacity: 0
            })),
  
            state('show', style({
                opacity: 1
            }))
        ])
    ],
})
  
export class AppComponent {
    emailValidate: RegExp =
         /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
    gfg: string;
}


  • 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