Online Playground
Browse tools

Validate a phone number in JavaScript: regex or library?

Should you validate phone numbers with a regex or a library? See where common regex patterns fail, with examples, and when each approach is the right choice.

Searching for a phone number regex gives you a hundred patterns and none of them is right for every country. This guide shows what a regex can and cannot do, and when a library like libphonenumber-js is worth the extra bytes.

The simple regex and where it breaks

A common pattern accepts ten digits. It works for one country and rejects everyone else.

js
const re = /^\d{10}$/
re.test('4155552671')      // true
re.test('+14155552671')   // false: the plus sign breaks it
re.test('020 7946 0958')  // false: spaces break it
re.test('0000000000')     // true: not a real number

// The library for the same input, with default country US:
parsePhoneNumberFromString('0000000000', 'US').isPossible() // true  (the length fits)
parsePhoneNumberFromString('0000000000', 'US').isValid()    // false (no such range exists)

A generic E.164 regex

If you only need to check the shape of a number that is already in E.164 form, this is a reasonable minimum.

js
const e164 = /^\+[1-9]\d{1,14}$/
e164.test('+442079460958') // true
e164.test('+0442079460958') // false

Why a per-country regex is better, and still not enough

The regex tool on this site gives you the pattern for each country from libphonenumber's data. It checks the length and prefix for that one country. It does not clean up input, tell you the number type or format the number, and it goes out of date when numbering plans change.

What a library adds

libphonenumber-js reads messy input, works out the country, and validates against up-to-date rules. It also gives you the E.164 value to store and the formats to show.

js
import { parsePhoneNumberFromString } from 'libphonenumber-js'

const phone = parsePhoneNumberFromString('020 7946 0958', 'GB')
phone.isValid() // true
phone.number    // '+442079460958'

isValid() or isPossible()

The library gives you two checks. isPossible() checks only that the length fits the country. isValid() also checks the digits against the assigned ranges. The library's author prefers isPossible() for forms, because numbering plans change and an old copy of the library could reject a new range. Use isValid() when the cost of a wrong number is high.

A practical rule

Use a regex for a quick shape check in a form, or in code that cannot take a dependency. Use a library when the number matters, for example before sending an SMS, saving it to a database or charging for verification.

FAQ

Questions people ask

What is the best regex to validate a phone number?

There is no single best one. For E.164 input use ^\+[1-9]\d{1,14}$. For a particular country, use that country's pattern from the regex generator.

Can a regex tell me if a number is real?

No. A regex checks the format only. Even a library can only say that a number fits the numbering plan, not that it is in service.

Is the library too heavy for a signup form?

The min build is about 42 KB gzipped, and it loads only where you need it. If that matters, load it on demand when the phone field is focused.