Skip to content

Commit e8c6914

Browse files
authored
fix(isTaxID): add formatted CPF support and additional test cases for pt-BR locale (#2675)
1 parent 90b0a9a commit e8c6914

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/lib/isTaxID.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,14 @@ function plPlCheck(tin) {
907907
}
908908

909909
/*
910-
* pt-BR validation function
911-
* (Cadastro de Pessoas Físicas (CPF, persons)
912-
* Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities)
913-
* Both inputs will be validated.
914-
* CNPJ supports both numeric (legacy) and alphanumeric format (starting July 2026).
915-
*/
910+
* pt-BR validation function
911+
* (Cadastro de Pessoas Físicas (CPF, persons)
912+
* Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities)
913+
* Both inputs will be validated.
914+
* CPF accepts formatted (XXX.XXX.XXX-XX) and unformatted input;
915+
* formatting is stripped before validation.
916+
* CNPJ supports both numeric (legacy) and alphanumeric format (starting July 2026).
917+
*/
916918

917919
/**
918920
* Convert a CNPJ character to its numeric value for check digit calculation.
@@ -966,6 +968,9 @@ function validateCnpj(cnpj) {
966968
}
967969

968970
function ptBrCheck(tin) {
971+
// Strip CPF formatting (XXX.XXX.XXX-XX)
972+
tin = tin.replace(/[.\-/]/g, '');
973+
969974
if (tin.length === 11) {
970975
let sum;
971976
let remainder;
@@ -1205,7 +1210,7 @@ const taxIdFormat = {
12051210
'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i,
12061211
'nl-NL': /^\d{9}$/,
12071212
'pl-PL': /^\d{10,11}$/,
1208-
'pt-BR': /(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i,
1213+
'pt-BR': /(?:^\d{3}\.\d{3}\.\d{3}-\d{2}$)|(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i,
12091214
'pt-PT': /^\d{9}$/,
12101215
'ro-RO': /^\d{13}$/,
12111216
'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/,

test/validators.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13957,6 +13957,13 @@ describe('Validators', () => {
1395713957
// CPF (persons)
1395813958
'35161990910',
1395913959
'74407265027',
13960+
'12345678909',
13961+
'11144477735',
13962+
'52998224725',
13963+
// CPF formatted (XXX.XXX.XXX-XX)
13964+
'123.456.789-09',
13965+
'111.444.777-35',
13966+
'529.982.247-25',
1396013967
// CNPJ numeric (legacy format)
1396113968
'05423994000172',
1396213969
'11867044000130',
@@ -13967,6 +13974,12 @@ describe('Validators', () => {
1396713974
invalid: [
1396813975
'ABCDEFGH',
1396913976
'170.691.440-72',
13977+
'000.000.000-00',
13978+
'111.111.111-11',
13979+
'123.456.789-00',
13980+
'12345678900',
13981+
'123',
13982+
'123456789012',
1397013983
'11494282142',
1397113984
'74405265037',
1397213985
'11111111111',

0 commit comments

Comments
 (0)