← Back to challenges

Validate Phone Numbers

PythonHardregexstringsvalidation

Instructions

Write a function that returns True if the phone number is in a valid format. Valid formats are as follows:

With Country CodeWithout Country Code
+1-892-445-7663892-445-7663
1-892-445-7663(892) 445-7663
1 (892) 445-7663892.567.8901
1.892.567.89011/892/567/8901
1 892 567 8901892/567/8901
18925678901892 567 8901

Examples

validate("578-332-1114") ➞ True

validate("57-332-1114") ➞ False

validate("577 332  1114") ➞ False
# More than one space in between digits clusters.

validate("57 332 1114") ➞ False
# Unacceptable digit cluster length.

validate("157%332-1114") ➞ False
# Unacceptable delimiter.

Notes

  • The country code will always be +1 or 1.
  • Each phone number will contain either 10 or 11 digits (depending on whether the country code exists).
  • There must either be exactly one space, a delimiter, or no space at all between the digit clusters.
  • You do not have to worry about extensions.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.