CPF and CNPJ in JavaScript and TypeScript
Validate, format and generate Brazilian tax IDs with the cpf library. Alphanumeric CNPJ support, built-in types and zero runtime dependencies.
01 / Input
Validate and format
Brazilian tax ID
Validation uses the normalized original input. The mask is a formatting preview.
Processed locally. No document number is sent.
02 / Output
Result
- Formatted preview
- —
Output of format
- Normalized value
- —
Enter a document number or load an example.
Validation checks the structure and check digits. It does not verify issuance, ownership or registration status.
In your project
Installation and usage
Available for Node.js and browsers, with ESM and CommonJS exports.
npm install cpfimport cpf, { cnpj } from 'cpf'
cpf.isValid('529.982.247-25')
// true
cnpj.isValid('12.ABC.345/01DE-35')
// true
cpf.format('5299822')
// '529.982.2'Documentation
Implementation guides
Validate CPF
Validate CPF with or without formatting, preserve leading zeros and diagnose errors with the cpf library for JavaScript and TypeScript.
Alphanumeric CNPJ
Validate numeric and alphanumeric CNPJ, normalize lowercase letters and calculate check digits in JavaScript and TypeScript.
Masks and forms
Format complete or partial CPF and CNPJ values while preserving the cursor and validating the original input.
React integration
Build a React form with CPF and CNPJ validation, controlled inputs, accessible messages and formatting on blur.