Skip to content

Commit

Permalink
Refactor: using regex to enhance phone validation
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniamaia committed Sep 21, 2023
1 parent 04619f6 commit 41ab7c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Gera um CEP válido aleatório.

### is_valid_phone

Verifica se o número de telefone é valido, podendo ser telefone fixo ou celular. Apenas números, com DDD e sem o prefixo internacional, formatados como string. ***Exemplo: +55 48 9999 9999 ficaria '4899999999'.*** Esta função valida apenas números de telefone brasileiros e não verifica se o número realmente existe.
Verifica se o número de telefone é valido, podendo ser telefone fixo ou celular. Apenas números, com DDD e sem o prefixo internacional, formatados como string. ***Exemplo: +55 48 9999 9999 deve ser chamado como is_valid_phone('4899999999').*** Esta função valida apenas números de telefone brasileiros e não verifica se o número realmente existe.



Expand Down
43 changes: 27 additions & 16 deletions brutils/phone.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

# FORMATTING
############

Expand All @@ -7,26 +9,35 @@


def is_valid_landline(phone_number): # type: (str) -> bool
return (
isinstance(phone_number, str)
and phone_number.isdigit()
and len(phone_number) == 10
and phone_number[0] != "0"
and phone_number[1] != "0"
and phone_number[2] in ["2", "3", "4", "5"]
)
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian landline numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.
"""
pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$")
return isinstance(phone_number, str) and re.match(pattern, phone_number)


def is_valid_mobile(phone_number): # type: (str) -> bool
return (
isinstance(phone_number, str)
and phone_number.isdigit()
and len(phone_number) == 11
and phone_number[0] != "0"
and phone_number[1] != "0"
and phone_number[2] == "9"
)
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian mobile numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.
"""
pattern = re.compile(r"^[1-9][1-9][9]\d{8}$")
return isinstance(phone_number, str) and re.match(pattern, phone_number)


def is_valid(phone_number): # type: (str) -> bool
"""
Returns whether or not the verifying first 3 digits are a
match.This function validates only Brazilian phone numbers
and does not verify if the number actually exists.
Input should be a digit string of proper length.
"""
return is_valid_landline(phone_number) or is_valid_mobile(phone_number)
2 changes: 1 addition & 1 deletion tests/test_phone.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class TestPhone(TestCase):
def test_is_valid_landline(self):
# When landline phone is not string, returns False
self.assertFalse(is_valid_landline(1938814933))
self.assertIs(is_valid_landline(1938814933), False)

# When landline phone doesn't contain only digits, returns False
self.assertFalse(is_valid_landline("(19)388149"))
Expand Down

0 comments on commit 41ab7c5

Please sign in to comment.