Regex Pattern Matching Phone Numbers
The first step in performing a Regex check with Python is to create a regex pattern that matches phone numbers. Then we will use the built-in Python Regex module to test the string that a user provides against the Regex pattern we have created.
Common Problems With Phone Number Validation
Similar to matching email addresses, matching phone numbers with Regex is not recommended in production. Phone number validation with a regular expression is tricky, particularly when your app must handle international numbers.
Number Length
As mentioned, valid US phone numbers are 10 digits long (when the area code is included and the +1 is not), but a valid Mexican phone number is 12 digits long. In some countries, phone numbers are longer. The maximum length for a international phone number is 15 digits, according to the International Telecommunication Union (ITU.)
Delimiters
Delimiters (the characters that come between the numbers in phone numbers) are also different all over the world. Some countries use the em-dash (-), while others use a period or dot (.) and still, others use parentheses, tildes, or other separators. The way numbers are grouped together in phone numbers also differs from country to country.
User Error
All these differences lead to a large margin for user error. Some users will try to include dashes in their phone numbers while others will not. Some users will include country codes. Some will include spaces whereas others will not. Some users might omit the area code or the +1 from a US number.
The Problem With Using Regex to Match Phone Numbers
All that being said, you will almost immediately come up against these problems when using a Regex pattern to match phone numbers because there are many different ways to format a phone number. Writing a single specific pattern to capture all the characters and possibilities is impossible.
For this reason, we don't recommend relying solely on Regex phone number pattern matching to handle parsing phone numbers and performing your phone number validation. Using a Regular expression in tandem with a dedicated third-party service is the most robust way to check for a valid number.
Parsing Phone Numbers
One recommended method for dealing with international phone numbers and other phone number matching issues is to strip all the characters that aren't numeric digits from the string before you begin.
Unfortunately, this method doesn't work very well either, as all you are left with after you remove anything that isn't a numeric digit is a string of numbers that could be between 7 and 15 characters long. This doesn't give you much to work with to determine whether or not the string is a valid number.
A Good Regex Pattern for Matching Phone Numbers
Let's take a look at some basic regular expression syntax that matches US phone numbers. Keep in mind that this pattern can only be relied upon to match US phone numbers. Here is the pattern:
This regular expression pattern breaks down as follows:
^
asserts the position at the start of a line.(\+1|1)?
matches an optional country code (+1 or 1) for US numbers.\s*
matches any number of whitespace characters (including none).\(?\d{3}\)?
matches an optional opening parenthesis, followed by exactly three digits, and an optional closing parenthesis.[\s.-]?
matches an optional space, period, or hyphen.\d{3}
matches exactly three digits.[\s.-]?
matches an optional space, period, or hyphen.\d{4}
matches exactly four digits.$
asserts the position at the end of a line.
Examples of US phone numbers that match this pattern:
123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+1 123-456-7890
1 123-456-7890
Let's look at how we would use this regular expression in our Python code:
In this Python code:
- We import the
re
module, which provides support for regular expressions. - We define the regular expression pattern using
re.compile()
. - We create a list of sample phone numbers to test.
- We define a function
validate_phone_number
that uses thepattern.match()
method to check if a phone number matches the pattern. - We loop through the sample phone numbers and print whether each one is valid or not.
This approach ensures that only properly formatted US phone numbers are considered valid.
Validating Phone Numbers With an API
Short of sending an SMS to a number to determine its validity (which you should always do as part of your phone number verification process) the best way to check for valid or invalid phone numbers is to use a dedicated third-party library or API.
One such service is AbstractAPI's Free Phone Number Verification API. This API provides an endpoint to which you can send a phone number string and receive a response telling you whether or not the string is a valid phone number.
AbstractAPI performs several checks to determine the validity of the number. First, they check the number against a regular expression. Next, they verify the number against a regularly updated database of phone numbers to determine whether the number is a disposable number, VOIP, a free phone number, or another type of low-quality number.
The API returns a JSON object a validity boolean and information about the number's carrier and location information.
Let's look at how we can use AbstractAPI's Free Phone Validation endpoint to check the validity of a provided number.
Acquire an API Key
Go to the Phone Validation API Get Started page and click the blue “Get Started” button.
You’ll need to sign up with an email address and password to acquire an API key. If you already have an AbstractAPI account, you'll be asked to log in. Next, you’ll land on the API’s homepage, where you’ll see options for documentation, pricing, and support.
Look for your API key, which will be listed on this page. Every Abstract API has a unique key, so even if you’ve used a different Abstract API endpoint before, this key will be unique.
Send a Request to the API
We'll use the built-in Python requests module to send a POST request to the API endpoint with the number for validation.
The response sent back by the API will look something like this:
From here, all we need to do is extract the valid field from the response object and use its value to determine the validity of the number.