Angular directive to validate CPF/CNPJ numbers, using cpf_cnpj.js.
To install this library, run:
$ npm install ng2-cpf-cnpj --save$ npm install ng2-cpf-cnpjand add to your app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { CpfCnpjModule } from 'ng2-cpf-cnpj';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
CpfCnpjModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }Just add [cpf] or [cnpj] in your inputs:
<input type="text" name="cpf" [(ngModel)]="cpf" [cpf]="cpf"/>
<input type="text" name="cnpj" [(ngModel)]="cnpj" [cnpj]="cnpj" />You can use reference to detect the error type:
<input type="text" name="cpf" [(ngModel)]="cpf" [cpf]="cpf" #cpfInput="ngModel" required />
<div *ngIf="cpfInput?.errors?.cpf">CPF Invalid</div>
<div *ngIf="cpfInput?.errors?.required">CPF Required</div>To generate all *.js, *.d.ts and *.metadata.json files:
$ npm run buildTo lint all *.ts files:
$ npm run lintMIT © Paulo Menezes