Skip to content

Commit d37bc8c

Browse files
committed
feat(isTaxID): add Brazilian CPF validation for pt-BR locale
- Update pt-BR regex to accept formatted CPF (XXX.XXX.XXX-XX) - Add formatting removal in ptBrCheck before validation - Reject all-equal-digit CPFs - Add comprehensive tests for valid and invalid CPFs Closes #2645
1 parent d109441 commit d37bc8c

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/lib/isTaxID.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,9 @@ function validateCnpj(cnpj) {
966966
}
967967

968968
function ptBrCheck(tin) {
969+
// Strip CPF formatting (XXX.XXX.XXX-XX)
970+
tin = tin.replace(/[.\-/]/g, '');
971+
969972
if (tin.length === 11) {
970973
let sum;
971974
let remainder;
@@ -1205,7 +1208,7 @@ const taxIdFormat = {
12051208
'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i,
12061209
'nl-NL': /^\d{9}$/,
12071210
'pl-PL': /^\d{10,11}$/,
1208-
'pt-BR': /(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i,
1211+
'pt-BR': /(?:^\d{3}\.\d{3}\.\d{3}-\d{2}$)|(?:^\d{11}$)|(?:^[A-Z0-9]{12}\d{2}$)/i,
12091212
'pt-PT': /^\d{9}$/,
12101213
'ro-RO': /^\d{13}$/,
12111214
'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
@@ -13997,6 +13997,13 @@ describe('Validators', () => {
1399713997
// CPF (persons)
1399813998
'35161990910',
1399913999
'74407265027',
14000+
'12345678909',
14001+
'11144477735',
14002+
'52998224725',
14003+
// CPF formatted (XXX.XXX.XXX-XX)
14004+
'123.456.789-09',
14005+
'111.444.777-35',
14006+
'529.982.247-25',
1400014007
// CNPJ numeric (legacy format)
1400114008
'05423994000172',
1400214009
'11867044000130',
@@ -14007,6 +14014,12 @@ describe('Validators', () => {
1400714014
invalid: [
1400814015
'ABCDEFGH',
1400914016
'170.691.440-72',
14017+
'000.000.000-00',
14018+
'111.111.111-11',
14019+
'123.456.789-00',
14020+
'12345678900',
14021+
'123',
14022+
'123456789012',
1401014023
'11494282142',
1401114024
'74405265037',
1401214025
'11111111111',

0 commit comments

Comments
 (0)