diff --git a/src/lib/isTaxID.js b/src/lib/isTaxID.js index 78eaa096e..3a87fd0b9 100644 --- a/src/lib/isTaxID.js +++ b/src/lib/isTaxID.js @@ -907,12 +907,14 @@ function plPlCheck(tin) { } /* -* pt-BR validation function -* (Cadastro de Pessoas Físicas (CPF, persons) -* Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities) -* Both inputs will be validated. -* CNPJ supports both numeric (legacy) and alphanumeric format (starting July 2026). -*/ + * pt-BR validation function + * (Cadastro de Pessoas Físicas (CPF, persons) + * Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities) + * Both inputs will be validated. + * CPF accepts formatted (XXX.XXX.XXX-XX) and unformatted input; + * formatting is stripped before validation. + * CNPJ supports both numeric (legacy) and alphanumeric format (starting July 2026). + */ /** * Convert a CNPJ character to its numeric value for check digit calculation. @@ -966,6 +968,9 @@ function validateCnpj(cnpj) { } function ptBrCheck(tin) { + // Strip CPF formatting (XXX.XXX.XXX-XX) + tin = tin.replace(/[.\-/]/g, ''); + if (tin.length === 11) { let sum; let remainder; @@ -1205,7 +1210,7 @@ const taxIdFormat = { 'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i, 'nl-NL': /^\d{9}$/, 'pl-PL': /^\d{10,11}$/, - 'pt-BR': /(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i, + 'pt-BR': /(?:^\d{3}\.\d{3}\.\d{3}-\d{2}$)|(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i, 'pt-PT': /^\d{9}$/, 'ro-RO': /^\d{13}$/, 'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/, diff --git a/test/validators.test.js b/test/validators.test.js index 4af573fda..56df5c1f0 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -13957,6 +13957,13 @@ describe('Validators', () => { // CPF (persons) '35161990910', '74407265027', + '12345678909', + '11144477735', + '52998224725', + // CPF formatted (XXX.XXX.XXX-XX) + '123.456.789-09', + '111.444.777-35', + '529.982.247-25', // CNPJ numeric (legacy format) '05423994000172', '11867044000130', @@ -13967,6 +13974,12 @@ describe('Validators', () => { invalid: [ 'ABCDEFGH', '170.691.440-72', + '000.000.000-00', + '111.111.111-11', + '123.456.789-00', + '12345678900', + '123', + '123456789012', '11494282142', '74405265037', '11111111111',