Validador-BR Documentation

Validate Brazilian documents easily

Learn how to use the library to validate CPFs, CNPJs, CNHs, PIS and more.

NPM VersionNPM Downloads

Getting Started

About

@box4dev/validador-br

A lightweight and robust library for validating Brazilian documents.

The library aims to ensure that documents comply with the mathematical and check digit calculations stipulated by the institutions.

Installation

To install the package in your project, run one of the commands below:

NPM

Shell
1
npm install @box4dev/validador-br

Yarn

Shell
1
yarn add @box4dev/validador-br

PNPM

Shell
1
pnpm add @box4dev/validador-br

Import

Named exports (recommended)

Use named functions to import only what you need.

JS
1
2
3
import { isValidCpf, isValidCnpj } from '@box4dev/validador-br';
console.log(isValidCpf('123.456.789-09')); // true ou false
console.log(isValidCnpj('GY.HU8.PG2/971E-10')); // true ou false

validate object (centralized)

Or use the `validate` object which centralizes all validators.

JS
1
2
3
import { validate } from '@box4dev/validador-br';
console.log(validate.cpf('123.456.789-09')); // true ou false
console.log(validate.cnpj('12.345.678/0001-95')); // true ou false

CommonJS (require)

JS
1
2
3
const { isValidCpf, isValidCnpj, validate } = require('@box4dev/validador-br');
console.log(isValidCpf('12345678909')); // true ou false
console.log(validate.cnpj('GYHU8PG2971E10')); // true ou false

Validators

CPF

Validates the format and check digits of the CPF (modulus 11).

JS
1
2
3
import { isValidCpf } from '@box4dev/validador-br';
console.log(isValidCpf('741.535.041-30')); // true ou false
console.log(isValidCpf('43241612163')); // true ou false

CNPJ / Alphanumeric CNPJ

Validates CNPJs in the old standard (numbers only) and the new alphanumeric standard.

JS
1
2
3
4
5
import { isValidCnpj } from '@box4dev/validador-br';
console.log(isValidCnpj('GY.HU8.PG2/971E-10')); // true ou false
console.log(isValidCnpj('12.345.678/0001-95')); // true ou false
console.log(isValidCnpj('AYWO9JCVCHFB47')); // true ou false
console.log(isValidCnpj('47434563000151')); // true ou false

CNH

Validates the National Driver's License using the official algorithm.

JS
1
2
import { isValidCnh } from '@box4dev/validador-br';
console.log(isValidCnh('12345678901')); // true ou false

PIS / PASEP / NIT

Validates the Worker Identification Number (PIS/PASEP/NIT).

JS
1
2
3
import { isValidPis } from '@box4dev/validador-br';
console.log(isValidPis('589.21704.72.2')); // true ou false
console.log(isValidPis('07987894517')); // true ou false

CNS

Validates provisional CNS numbers (starting with 7, 8, 9) and definitive ones (1, 2).

JS
1
2
3
import { isValidCns } from '@box4dev/validador-br';
console.log(isValidCns('252 9608 1932 0002')); // true ou false
console.log(isValidCns('146471200460007')); // true ou false

Voter ID

Validates the voter ID by checking digits and state code.

JS
1
2
3
import { isValidTituloEleitor } from '@box4dev/validador-br';
console.log(isValidTituloEleitor('4113 4513 0930')); // true ou false
console.log(isValidTituloEleitor('428779582410')); // true ou false

Certificate

Validates the 32-digit registration number of certificates (Birth, Marriage, Death).

JS
1
2
3
import { isValidCertidao } from '@box4dev/validador-br';
console.log(isValidCertidao('106836 11 1926 1001 1 88626 162 6561443')); // true ou false
console.log(isValidCertidao('81007223197120011105781766173474')); // true ou false

State Registration

Validates specific State Registrations for each state (UF). Requires the state abbreviation.

JS
1
2
3
4
import { isValidIE } from '@box4dev/validador-br';
// valor da IE e UF
console.log(isValidIE('7122026812064', 'SP')); // true ou false
console.log(isValidIE('01.004.823/001-12', 'AC')); // true ou false

Credit Card

Validates cards (Visa, Mastercard, Amex, etc.) using the Luhn Algorithm.

JS
1
2
3
import { isValidCartaoCredito } from '@box4dev/validador-br';
console.log(isValidCartaoCredito('4389 3536 3524 6297')); // true ou false
console.log(isValidCartaoCredito('4389351453183261')); // true ou false

Frequently Asked Questions