Guides
Last updated
June 5, 2024

How to Validate a Phone Number with Python (Regex)

Elizabeth (Lizzie) Shipton

Table of Contents:

Get your free
Phone Validation
API key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

The first step in using phone numbers in user authentication is verifying the phone numbers. This means determining that the string a user provided in the input is, in fact, a phone number and that it is an active number capable of receiving texts and calls.

In this tutorial, we'll look at using Regex and Python to verify that a given string matches what we expect a phone number to look like. We will then use a free API service to find out whether the phone number is currently active, and get some information about the number, including carrier and location.

This tutorial will not cover using a service like Twilio to send a code to a mobile device to determine that it belongs to the user. It will also not cover the basics of getting started with Python. If you need a refresher on Python and how to get started, we recommend checking the docs.

Let’s send your first free
Phone Validation
call
See why the best developers build on Abstract
Get your free api

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:

^(\+1|1)?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

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:



import re

# Define the regular expression pattern
pattern = re.compile(r'^(\+1|1)?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$')

# Sample phone numbers to test
phone_numbers = [
    "123-456-7890",
    "(123) 456-7890",
    "123 456 7890",
    "123.456.7890",
    "+1 123-456-7890",
    "1 123-456-7890"
]

# Function to validate phone numbers
def validate_phone_number(phone_number):
    if pattern.match(phone_number):
        return True
    else:
        return False

# Test the phone numbers
for number in phone_numbers:
    if validate_phone_number(number):
        print(f"{number} is a valid US phone number.")
    else:
        print(f"{number} is NOT a valid US phone number.")

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 the pattern.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.

Elizabeth (Lizzie) Shipton

Lizzie Shipton is an adept Full Stack Developer, skilled in JavaScript, React, Node.js, and GraphQL, with a talent for creating scalable, seamless web applications. Her expertise spans both frontend and backend development, ensuring innovative and efficient solutions.

Get your free
Phone Validation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
Phone Validation
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required