Online Playground
Browse tools

Phone Number Regex Generator

Copy a phone number validation regex for any country, with code for JavaScript, Python and PHP.

Get a phone number regex

National number regex digits only, without the country code
International (E.164) regex with + and the country code

Possible lengths:

JavaScript
Python
PHP

Processed locally with libphonenumber-js. Nothing you enter is sent to a server.

How it works

Three steps, all in your browser

  1. 1

    Pick a country

    The pattern comes from the numbering plan data used by Google's libphonenumber, not from a guess.

  2. 2

    Test a number

    Type any number and see at once whether it matches.

  3. 3

    Copy the code

    Take the regex alone, or a ready snippet for JavaScript, Python or PHP.

In depth

Four checks on the same six inputs

This table is the honest answer to "should I use a regex?". It ran four checks on six real inputs: the common ten-digit regex, the generic E.164 regex, the US-specific pattern from libphonenumber's data, and the library itself with the US as default country.

Input^\d{10}$E.164 regexUS pattern + codeLibrary isValid()
4155552671truefalsefalsetrue
+14155552671falsetruetruetrue
(415) 555-2671falsefalsefalsetrue
+44 20 7946 0958falsefalsefalsetrue
0000000000truefalsefalsefalse
+1 415 555 267falsefalsefalsefalse

How to read the table

  • The ten-digit regex accepts 0000000000, which is not a real number, and rejects every number with a plus sign or brackets.
  • The generic E.164 regex only checks the shape, so it accepts any country and any digits as long as the plus sign and length are right.
  • The country pattern checks the digits for one country, but it cannot clean up input like (415) 555-2671 first.
  • The library cleans the input, finds the country and validates, which is why it is the only column that handles all six.

Where the country patterns come from

The patterns on this page are read straight from libphonenumber-js's own metadata, which is derived from Google's numbering plan data, not written by hand. They match the national number only, so the international pattern adds the plus sign and the country code in front. Numbering plans change, so regenerate the pattern when you update the library.

When a regex is the right tool

Use a regex when you need an instant shape check in a form field, or when you cannot add a dependency. For anything that sends a message, saves a record or costs money, use the library.

FAQ

Questions people ask

What is the best regex for phone numbers?

There is no single one. A generic check such as ^\+[1-9]\d{1,14}$ only tests the E.164 shape. For real validation, use the pattern for the country, and ideally a library such as libphonenumber, which also checks length rules and number types.

Does the regex check the number is in service?

No. It checks the number fits the numbering plan: right length and an assigned prefix range. A number can match and still be unassigned or disconnected.

Why not just use a regex in production?

Numbering plans change often. A regex copied today will go out of date, whereas a library is updated with the data. Use these patterns for quick form checks and tests.