The last two digits of a CNPJ are not chosen: they are computed from the first twelve. That is what lets a system catch a mistyped number on the spot, without querying any database. With the alphanumeric CNPJ arriving in 2026, that calculation also needs to handle letters — and this guide shows exactly how.
Below is the complete algorithm, the character conversion table, a worked example and a JavaScript function that validates both the numeric and the alphanumeric format. If you only want to check a number, use the Alphanumeric CNPJ Validator, which shows each step of the calculation on screen.
The algorithm: modulo 11, step by step
The CNPJ check digit (DV) uses the modulo 11 algorithm. It is computed in two stages — one for each digit — always following the same steps:
- Convert each character of the base into a number (the conversion rule is in the next section).
- Multiply each value by a weight, which goes from 2 to 9 from right to left and restarts at 2 when it passes 9.
- Sum all the products.
- Take the remainder of that sum divided by 11. If the remainder is below 2, the digit is 0; otherwise the digit is 11 minus the remainder.
The first DV uses the 12 positions of the base. The second DV repeats exactly the same process, but including the first digit already computed as the 13th character.
The character conversion table
What changes in the alphanumeric CNPJ is only how each character becomes a number. The rule is simple and single: take the ASCII code of the character and subtract 48. That keeps the digits intact and gives a consistent value to each letter.
| Character | Value in the calculation |
|---|---|
| 0 to 9 | 0 to 9 (unchanged) |
| A, B, C, D … | 17, 18, 19, 20 … |
| … W, X, Y, Z | … 39, 40, 41, 42 |
Fonte: Alphanumeric CNPJ specification (Serpro / Receita Federal).
Worked example: 12.ABC.345/01DE-35
Let us compute the digits of the base 12ABC34501DE, the official example. Each character becomes a value (1, 2, 17, 18, 19, 3, 4, 5, 0, 1, 20, 21), which is multiplied by its position weight and summed.
- 1st digit: the sum of the products is 459. The remainder of 459 by 11 is 8. Since 8 is greater than 2, the digit is 11 − 8 = 3.
- 2nd digit: repeating the math with the 3 included (12ABC34501DE3), the sum is 424. The remainder of 424 by 11 is 6, so the digit is 11 − 6 = 5.
Putting the two together, the full CNPJ is 12.ABC.345/01DE-35. You can check this same step-by-step, with the weights and products table, by opening “Show the check-digit calculation” in the validator.
Ready-to-use JavaScript
The function below validates a numeric or alphanumeric CNPJ. It strips punctuation, separates the base from the check digits, recomputes the DV with modulo 11 and compares it with the one provided:
function checkDigit(base) {
let sum = 0;
let weight = 2;
for (let i = base.length - 1; i >= 0; i--) {
sum += (base.charCodeAt(i) - 48) * weight;
weight = weight === 9 ? 2 : weight + 1;
}
const rest = sum % 11;
return rest < 2 ? 0 : 11 - rest;
}
function isValidCnpj(value) {
const cnpj = value.toUpperCase().replace(/[^0-9A-Z]/g, '');
if (cnpj.length !== 14) return false;
const base = cnpj.slice(0, 12);
const dv1 = checkDigit(base);
const dv2 = checkDigit(base + dv1);
return cnpj.slice(12) === String(dv1) + String(dv2);
}
isValidCnpj('12.ABC.345/01DE-35'); // trueNotice that the function works for both formats without branching: since digits 0 to 9 also go through the ASCII − 48 conversion (and still count as 0 to 9), the numeric CNPJ is just a special case of the same algorithm.
Common implementation mistakes
- Using parseInt on letters. Letters are not digits — the correct conversion is the ASCII code minus 48, not a numeric parse.
- Forgetting to include the 1st check digit when computing the 2nd. The second digit always uses 13 characters: the base plus the first digit.
- Accepting check digits with a letter. The first 12 positions may have letters, but the last 2 characters are always numeric.
- Storing the CNPJ as a number. Leading zeros and letters are lost — always treat it as 14-character text.
Validate and generate without reimplementing
If you would rather not keep the algorithm by hand, you can use the ready-made tools: the Alphanumeric CNPJ Validator checks any number and shows the calculation, and the Alphanumeric CNPJ Generator creates valid test CNPJs, in bulk, to populate your staging environments. For the full picture of the change, see the guide on the new CNPJ format in 2026.
Referências e leitura complementar
See your first competitor in minutes
14-day free trial, no card. Within minutes, the first detection shows up on your dashboard.
Create free account