Open In App

Angular PrimeNG Form KeyFilter Properties 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 Properties 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.

Angular PrimeNG Form KeyFilter Properties:

  • pValidateOnly: The value in the input field is checked against the given regular expression.

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 Properties Component.

  • app.component.html:

HTML




<h1 style="color: green">
  GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter 
      Properties Component
</h5>
  
<form #form="ngForm">
    <input id="gfg" 
           type="text" 
           name="gfg" 
           [(ngModel)]="gfg" 
           pInputText 
           [pKeyFilter]="special" 
           [pValidateOnly]="true" 
           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 {
    special: 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: This is another basic example that shows how to use the Angular PrimeNG Form KeyFilter Properties Component.

  • app.component.html:

HTML




<h1 style="color: green">
  GeeksforGeeks
</h1>
<h5>
      Angular PrimeNG KeyFilter 
      Properties Component
</h5>
  
<form #form="ngForm">
    <input id="gfg" 
           type="text" 
           name="gfg" 
           [(ngModel)]="gfg" 
           pInputText 
           [pKeyFilter]="space" 
           [pValidateOnly]="true" 
           placeholder="Numbers not allowed" 
           style="margin-right: .5em">
    <p-message severity="error" 
               text="Numbers not allowed" 
               [@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 {
    space: RegExp = /^[A-Za-z]+$/;
    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