Guides
Last updated
July 20, 2025

5 Ways to Validate Phone Numbers with Regex

Nicolas Rios
Nicolas Rios
Table of Contents:
ON THIS PAGE
Get your free
phone validation
 API key now
stars rating
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

Validating phone numbers with regular expressions is a frequent challenge. We'll explore five different regex approaches, complete with code snippets, to tackle this. We will also examine the common pitfalls of these methods and see how Abstract API provides a more robust solution for handling them.

How to Implement Phone Number Validation in Regex

Regular expressions offer several ways to check phone number formats. Below are four distinct methods, from strict international standards to flexible, multi-step approaches for different validation needs.

E.164-Only Validation

This method validates numbers against the ITU E.164 standard. The pattern looks for a mandatory plus sign, a first digit that is not zero, and then between one and fourteen additional digits. This approach works for any country because it confirms the international format, not a specific national plan. A Stack Overflow discussion provides the pattern.

e164 = re.compile(r'^\+[1-9]\d{1,14}$')
if e164.fullmatch(raw): …

Free-Form International Validation

This pattern accommodates various international formats. It supports an optional country code of one to three digits, an optional trunk digit '1', and parentheses around the area code. It also permits spaces, dots, or hyphens as separators and allows for variable subscriber number lengths. This pattern is detailed in a phone regex list.

const intl = new RegExp(String.raw`^(\+\d{1,3}\s?)?…$`);
if (intl.test(raw)) { … }

Strict North American Numbering Plan Validation

This regex is for the North American Numbering Plan (NANP), used in the US and Canada. It allows for an optional "+1" prefix. The pattern ensures the three-digit area code does not start with a zero or one. It also handles optional parentheses around the area code and validates a consistent separator to enforce exactly ten digits. A similar US pattern is available for reference.

reNANP := regexp.MustCompile(`^(?:\+1\s?)?(?:\([2-9]\d{2}\)|[2-9]\d{2})[\s.-]?\d{3}[\s.-]?\d{4}$`)
reNANP.MatchString(raw)

Two-Pass Normalization and Validation

This technique uses two steps. First, a regex pass strips any character that is not a digit, or a plus sign at the start of the string. This converts the input into a clean, canonical format.

The second step applies business rules or another regex to the normalized string. For example, the logic can check if the clean string matches the E.164 format or a 10-digit NANP structure. This is effective when user input comes in many formats but must be stored uniformly.

# Step 1
clean = re.sub(r'(?!^\+)[^\p{Nd}]', '', raw, flags=re.U)

# Step 2
if clean.startswith('+'):
    ok = bool(re.fullmatch(r'\+[1-9]\d{1,14}', clean))
elif len(clean) == 10 and clean[0] in '23456789':
    ok = True
else:
    ok = False
    

The Challenges of Regex Phone Number Validation

While regex can check a number's format, it falls short in semantic validation. This approach introduces several practical problems that can lead to incorrect results and a poor user experience.

  • National numbering plans change often. A pattern like the Strict NANP method becomes obsolete with new area codes or length rules, which causes it to reject valid numbers over time.
  • Regex only confirms a number's structure, not its status. A number can match a pattern, like the Free-form international one, yet belong to an unassigned or retired block, a fact regex cannot know.
  • Real-world numbers contain extra characters like extensions or vanity text. A single, comprehensive regex to handle this noise, like the Free-form international pattern, becomes unwieldy and fragile.
  • Strict formats like E.164-only validation reject common national formats that users enter with spaces or parentheses. This creates false negatives and frustrates users who input otherwise valid numbers.

Validate Phone Numbers with Abstract API
Implement proper phone number validation on your forms to maintain high data quality and reliability.
Get started for free

How Abstract API Handles Phone Number Validation

Abstract API addresses the core weaknesses of traditional regex validation. It consults a curated numbering-plan database and real-time carrier feeds to verify numbers.

  • While regex only inspects a string for a pattern, Abstract API confirms if a number actually exists. It returns a simple boolean flag plus rich metadata like carrier, line type, and location.
  • The API owns the formatting rules. Your code never needs to chase updates for more than 190 country patterns or handle complex edge cases.
  • The response exposes a risk score and detects disposable or VoIP numbers. This is information that regex cannot infer from a string pattern alone.
  • Low latency and enterprise-grade uptime eliminate operational overhead. You no longer need to ship regex updates across different services or client apps.

How to Bring Abstract API to Your Dev Environment

Once you're familiar with Abstract’s capabilities, the addition of its phone number validation API to your project is simple.

  • Create a free account at Abstract API and copy your unique Phone Validation API key.
  • Add the key to your secure configuration, for example, export ABSTRACT_API_KEY=xxx in your secrets manager.
  • Install an HTTP client like axios, node-fetch, or requests.
  • Construct the request URL: https://phonevalidation.abstractapi.com/v1/?api_key=$ABSTRACT_API_KEY&phone={E164orRaw}.
  • Call the endpoint from your service and parse the JSON response.
  • Control downstream flows, such as dialers or signups, based on the response.valid field and other optional data.
const fetch = (await import('node-fetch')).default;
async function validate(phone) {
  const url = `https://phonevalidation.abstractapi.com/v1/?api_key=${process.env.ABSTRACT_API_KEY}&phone=${encodeURIComponent(phone)}`;
  const res = await fetch(url);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return await res.json();
}

Sample Phone Number Validation Implementation with Abstract API

The Node.js code sends a phone number to the API endpoint. The API then returns a detailed JSON object with validation results and rich metadata. Below is a sample response for a valid US mobile number.

The response fields provide deep insight. The `valid` field confirms existence. The `format` object lets you standardize display. The `country` and `prefix` fields enable geo-specific logic, while `location` and `carrier` help detect data mismatches. Finally, the `type` field specifies if the number is mobile, landline, or VoIP to guide your contact strategy.

{
  "phone": "14152007986",
  "valid": true,
  "format": {
    "international": "+14152007986",
    "local": "(415) 200-7986"
  },
  "country": {
    "code": "US",
    "name": "United States",
    "prefix": "+1"
  },
  "location": "California",
  "type": "mobile",
  "carrier": "T-Mobile USA, Inc."
}

Final Thoughts

Traditional regex validation only checks a string's pattern and cannot confirm if a number actually exists. This method requires constant maintenance for new country codes and formats. Abstract API overcomes these limits with real-time carrier checks and a managed database. To reliably validate user phone numbers, consider the creation of an account on Abstract API for your free API key.

Validate Phone Numbers with Abstract API
Validate phone numbers to maintain data integrity and ensure you can reliably contact your users.
Get started for free

Related Articles

Phone Validation
key now
Get your free
stars rating
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