diff options
| author | Bobby <[email protected]> | 2022-08-31 00:04:23 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-08-31 00:04:23 -0400 |
| commit | c6344afd77b8bf5baeb67fee8270ca24c555ec6c (patch) | |
| tree | a0e7151ec44c05969ae95ee6c969980160577d1c | |
| parent | d909b5d264b7244dbf7d74970e4b34be9181bb9b (diff) | |
| download | edify-c6344afd77b8bf5baeb67fee8270ca24c555ec6c.tar.xz edify-c6344afd77b8bf5baeb67fee8270ca24c555ec6c.zip | |
Added Phone Number Validation
| -rw-r--r-- | src/edify/library/__init__.py | 2 | ||||
| -rw-r--r-- | src/edify/library/phone.py | 17 | ||||
| -rw-r--r-- | tests/test_phone.py | 24 |
3 files changed, 43 insertions, 0 deletions
diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index 88f67d1..549cd4c 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -1,3 +1,5 @@ # flake8: noqa +# Import everything from the library. +from .phone import phone_number from .mail import email diff --git a/src/edify/library/phone.py b/src/edify/library/phone.py new file mode 100644 index 0000000..5c271af --- /dev/null +++ b/src/edify/library/phone.py @@ -0,0 +1,17 @@ +import re + +pattern = "^\\+?\\d{1,4}?[-.\\s]?\\(?\\d{1,3}?\\)?[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,9}$" + +def phone_number(phone: str) -> bool: + """Checks if a string is a valid phone number. + + Args: + phone (str): The string to check. + Returns: + bool: True if the string is a valid phone number, False otherwise. + """ + + if re.match(pattern, phone): + return True + else: + return False diff --git a/tests/test_phone.py b/tests/test_phone.py new file mode 100644 index 0000000..6a26af5 --- /dev/null +++ b/tests/test_phone.py @@ -0,0 +1,24 @@ +from edify.library import phone_number + +def test(): + phones = { + "1234567890": True, + "123 456 7890": True, + "123-456-7890": True, + "123.456.7890": True, + "123 456 7890": True, + "+1 (123) 456-7890": True, + "+1 (123) 456 7890": True, + "+1 (123) 456-7890": True, + "+102 (123) 456-7890": True, + "+91 (123) 456-7890": True, + "90122121": True, + "12345678901": True, + "+1 (124) 232": True, + "+1 (123) 45-890": True, + "+1 (1) 456-7890": True, + "9012": False, + "+1 (615) 243-": False + } + for phone, expectation in phones.items(): + assert phone_number(phone) == expectation |
