From e1f83be4f346eee70bf677f10a5718572819b19a Mon Sep 17 00:00:00 2001 From: Felipe Trevisan Date: Fri, 10 Aug 2018 19:25:14 -0300 Subject: [PATCH] Improving pt-BR mobile phone validation --- lib/isMobilePhone.js | 2 +- src/lib/isMobilePhone.js | 2 +- test/validators.js | 21 ++++++++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/isMobilePhone.js b/lib/isMobilePhone.js index 8af5d2835..ac3c3d8ac 100644 --- a/lib/isMobilePhone.js +++ b/lib/isMobilePhone.js @@ -64,7 +64,7 @@ var phones = { 'nl-BE': /^(\+?32|0)4?\d{8}$/, 'nn-NO': /^(\+?47)?[49]\d{7}$/, 'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/, - 'pt-BR': /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/, + 'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/, 'pt-PT': /^(\+?351)?9[1236]\d{7}$/, 'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/, 'ru-RU': /^(\+?7|8)?9\d{9}$/, diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index 72cdabd17..192ab1825 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -53,7 +53,7 @@ const phones = { 'nl-BE': /^(\+?32|0)4?\d{8}$/, 'nn-NO': /^(\+?47)?[49]\d{7}$/, 'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/, - 'pt-BR': /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/, + 'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/, 'pt-PT': /^(\+?351)?9[1236]\d{7}$/, 'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/, 'ru-RU': /^(\+?7|8)?9\d{9}$/, diff --git a/test/validators.js b/test/validators.js index 1f2416691..d45b49a7b 100644 --- a/test/validators.js +++ b/test/validators.js @@ -3451,14 +3451,25 @@ describe('Validators', () => { { locale: 'pt-BR', valid: [ - '55-17-3332-2155', - '55-15-25661234', - '551223456789', - '01523456987', - '022995678947', '+55-12-996551215', + '+55-15-97661234', + '55-17-96332-2155', + '55-17-6332-2155', + '55-15-976612345', + '55-15-75661234', + '+5512984567890', + '+551283456789', + '5512984567890', + '551283456789', + '015994569878', + '01593456987', + '022995678947', + '02299567894', ], invalid: [ + '0819876543', + '08158765432', + '+55-15-7566123', '+017-123456789', '5501599623874', '+55012962308',