Componente para validação de CPF e CNPJ em aplicações Angular (10+).
npm install ngx-cnpj-cpf-validatorComponente para validação de CPF e CNPJ em aplicações Angular (10+).
``shell`
npm install ngx-cnpj-cpf-validator --save
#### Formulários reativos
app.component.html (Utiliza biblioteca PO-UI)
`html`
app.component.ts
`javascript
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { CnpjCpfValidator, TipoPessoa } from 'cnpj-cpf-validator';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
public form: FormGroup;
public ngOnInit(): void {
this.form = new FormGroup({
cnpj: new FormControl(
'',
[],
[CnpjCpfValidator(TipoPessoa.PESSOA_JURIDICA)]
),
cpf: new FormControl(
'',
[],
[CnpjCpfValidator(TipoPessoa.PESSOA_FISICA)]
),
});
}
}
`
#### Validação em eventos
app.component.html
`html`
app.component.this
`javascript
import { Component, OnInit } from '@angular/core';
import { validaCPF } from 'cnpj-cpf-validator';
@Component({
selector: 'app-valida-cpf', templateUrl: './app.component.html',
styleUrls: ['./valida-cpf.component.css'], })
export class AppComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
public onValidateCPF(value: string) {
if (validaCPF(value)) {
alert('CPF válido');
} else {
alert('CPF inválido');
}
}
}
`
#### Funções disponíveis
`javascript``
- validaCPF(value: string): boolean
- validaCNPJ(value: string): boolean
- CnpjCpfValidator(tipoPessoa: TipoPessoa): AsyncValidatorFn