Open In App

Angular PrimeNG Form KeyFilter Validate Mode 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 learn how to use the KeyFilter Validate Mode 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. To enable the Validate Mode, the pValidateOnly property can be utilized which will validate the entire input data with a built-in Angular validator.



Syntax:

<form #formData="ngForm"> 
   <input type="text" 
             [(ngModel)]="..." 
             pInputText [pKeyFilter]="ccRegex" 
             [pValidateOnly]="true">
   <p-message severity="error" 
                 [@errorState]=
                 "form.dirty && !form.valid ? 'visible' : 'hidden'">
   </p-message>
</form>

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 Validate Mode Component.




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




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;
}




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 Validate Mode Component.




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




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;
}




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 :