Use alphanumeric CNPJ in v2
mainVersion 2.0.0 introduces support for the alphanumeric CNPJ format.
- To generate an alphanumeric CNPJ, pass
2as the argument togenerateCnpj(2). - To validate an alphanumeric CNPJ, you must explicitly pass
{ version: 2 }in the options object. By default,isValidCnpj()only validates numeric (version 1) CNPJs.
Note on generateCnpj behavior: In v2.x, generateCnpj() defaults to version 1. In v3.0.0, this will change to a random selection between version 1 and 2. To ensure consistent results, always specify the version explicitly.
import { isValidCnpj, generateCnpj } from '@brazilian-utils/brazilian-utils';
// Generate alphanumeric CNPJ
const alphaCnpj = generateCnpj(2); // e.g., "Q0SLFMBD7VX439"
// Validate alphanumeric CNPJ (requires version option)
isValidCnpj("Q0.SLF.MBD/7VX4-39", { version: 2 }); // true
isValidCnpj("Q0SLFMBD7VX439", { version: 2 }); // true
// Version 1 (numeric) is the default
isValidCnpj("12.345.678/0001-95"); // true (validates numeric only)
isValidCnpj("12.345.678/0001-95", { version: 1 }); // true (explicit)