FmtDev
Idioma
Back to blog
March 26, 2026

Regex for Phone Numbers: The Complete Validation Guide

Copy-paste regex patterns for US and international phone number validation. Includes JavaScript, Python, Java examples and common mistakes.

Regex for Phone Numbers: The Complete Validation Guide

Phone number validation with regex is one of the most common developer tasks, and one of the most commonly done wrong. There is no single regex that validates all phone numbers perfectly across every country and format. This post gives you patterns that work for the most common cases, plus the right approach for production apps.

Quick Answer: Copy These Patterns

You probably need a working regex for phone number validation right now. Use these three patterns for your most common use cases.

US Phone Numbers (10 digits, any common format)

This pattern matches formats like (555) 123-4567, 555-123-4567, 5551234567, 555.123.4567, and +1 555 123 4567.

Regex Pattern:

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

JavaScript Example:

const usPhoneRegex = /^(?:\+1\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;
console.log(usPhoneRegex.test("(555) 123-4567")); // true
console.log(usPhoneRegex.test("555.123.4567")); // true

What it matches: +1 555 123 4567, (555) 123-4567, 555-123-4567. What it rejects: 555-123-456, (555)123-45678.

Test this pattern with your own numbers in our Regex Tester.

International Phone Numbers (E.164 Format)

E.164 is the international standard: a plus sign followed by the country code and number, totaling 7 to 15 digits. This is what Stripe, Twilio, and most APIs expect for reliable delivery.

Regex Pattern:

^\+[1-9]\d{1,14}$

JavaScript Example:

const e164Regex = /^\+[1-9]\d{1,14}$/;
console.log(e164Regex.test("+14155552671")); // true
console.log(e164Regex.test("+442071234567")); // true

Flexible International (Human-Readable Formats)

This pattern is more permissive. it matches numbers with spaces, dashes, parentheses, dots, and optional country codes. Use this for user input where you want to allow various styles.

Regex Pattern:

^\+?[1-9]\d{1,14}(?:[\s.-]\d{1,14})*$

JavaScript Example:

const flexibleRegex = /^\+?[1-9]\d{1,14}(?:[\s.-]\d{1,14})*$/;
console.log(flexibleRegex.test("+44 20 7123 4567")); // true

Warning: this is more permissive. It validates format, not whether the number actually exists.

Breaking Down the US Phone Number Regex

Understanding how your regex for phone number patterns works helps you modify them for specific needs. Let's break down the US pattern bit by bit.

  • ^ : Anchor for the start of the string.
  • (?:\+1\s?)? : Optional non-capturing group for "+1" followed by an optional space.
  • \(? : Optional opening parenthesis (escaped).
  • \d{3} : Exactly 3 digits for the area code.
  • \)? : Optional closing parenthesis.
  • [\s.-]? : Optional separator (space, dot, or dash).
  • \d{3} : 3 digits for the prefix.
  • [\s.-]? : Another optional separator.
  • \d{4} : 4 digits for the line number.
  • $ : Anchor for the end of the string.

You can paste this pattern into our Regex Tester and hover over each part to see a visual explanation.

Common Phone Number Formats Your Regex Should Handle

One regex is rarely enough because users enter data in dozens of ways. Here are the real formats developers encounter every day.

| Format | Example | Common In | |---|---|---| | (xxx) xxx-xxxx | (555) 123-4567 | US/Canada display | | xxx-xxx-xxxx | 555-123-4567 | US/Canada forms | | xxx.xxx.xxxx | 555.123.4567 | US/Canada alt | | xxxxxxxxxx | 5551234567 | Databases, APIs | | +1xxxxxxxxxx | +15551234567 | E.164 standard | | +xx xxxx xxxx | +44 7911 123456 | UK mobile | | +xx xxx xxx xxxx | +91 987 654 3210 | India | | +xx x xxxx xxxx | +61 4 1234 5678 | Australia |

Why Regex Alone Is Not Enough for Production

While regex is great for quick checks, it has hard limits. You need to know when to move beyond simple pattern matching.

What Regex Can Do

  • Validate FORMAT: check if it "looks" like a phone number.
  • Check LENGTH: ensure the right number of digits are present.
  • Enforce CONSISTENCY: match your expected design or database structure.

What Regex Cannot Do

  • Verify the number EXISTS.
  • Check if it is CURRENTLY IN SERVICE.
  • Validate the COUNTRY CODE is real.
  • Confirm the AREA CODE exists for that specific country.
  • Handle every international format variation (there are hundreds worldwide).

What to Use for Production Validation

For production-grade apps handling real users, you should use Google's libphonenumber library. it is available in JavaScript, Python, Java, PHP, and Ruby. This is what Google, Facebook, and Amazon use to ensure data integrity.

JavaScript Example (libphonenumber-js):

import { parsePhoneNumberFromString } from 'libphonenumber-js'
const phoneNumber = parsePhoneNumberFromString('+1 (555) 123-4567')
if (phoneNumber && phoneNumber.isValid()) {
  console.log(phoneNumber.number) // +15551234567
}

Use regex for quick client-side format checking to help the user, but use a dedicated library for server-side validation.

Phone Number Regex in Different Languages

The logic for your phone number validation regex remains the same, but the syntax for implementation changes slightly across languages.

JavaScript

const regex = /^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;
const isValid = regex.test(phoneNumber);

Python

import re
pattern = r"^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$"
is_valid = re.match(pattern, phone_number)

Java

Pattern pattern = Pattern.compile("^\\(?\\d{3}\\)?[\\s.-]?\\d{3}[\\s.-]?\\d{4}$");
boolean isValid = pattern.matcher(phoneNumber).matches();

PHP

$pattern = '/^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/';
$isValid = preg_match($pattern, $phoneNumber);

Common Mistakes in Phone Number Regex

Even senior developers make these mistakes when building validation patterns.

  • Forgetting to escape parentheses: ( and ) are regex special characters. You must use \( and \) to match the literal characters.
  • Not anchoring the pattern: Without ^ and $, a string like "call 555-123-4567 now" would match. Always anchor your patterns.
  • Being too strict: requiring one specific format (like dashes only) rejects valid numbers from users who use spaces or dots.
  • Being too loose: accepting any string of digits means nonsensical numbers like "000-000-0000" will pass.
  • Ignoring international users: hardcoding a US-only format excludes the vast majority of the world's population.

For quick format validation, the patterns above cover 90% of use cases. For production apps handling real users, combine regex with libphonenumber for the best results.

Test any of these patterns instantly in our Regex Tester. If you are building an API that handles these numbers, you can easily convert cURL to Python requests or format your output with our JSON Formatter to verify your validation logic.

Check out our guide on 5 regex patterns every developer should know for more practical examples.

Herramienta Asociada

¿Listo para usar la herramienta Probador Visual de Regex en Línea? Todo el procesamiento es local.

Abrir Probador Visual de Regex en Línea