Phone Number Regex Javascript Validator

Search...

⌘K

Phone Number Regex Javascript Validator

Search...

⌘K


Phone Number Regex Javascript Validator

Phone Number Regex Javascript Validator

The Phone Number Regex JavaScript Validator lets you instantly check if a number string follows a valid phone number format using JavaScript regex. It’s ideal for use in contact forms, signup flows, or any web app that captures phone inputs. Use this alongside our JavaScript Regex Tester to build and refine your own patterns, or pair it with the Email Regex JavaScript Validator for complete contact form validation. You can also encode validated numbers using the Base64 Encoder or generate secure tokens for phone-based login systems with the Token Generator.

(123) 456-7890
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "(123) 456-7890" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is Phone Number Regex?

Phone number regex is a regular expression pattern that checks if a string matches a valid phone number format. The pattern can vary based on:

  • Country format

  • Mobile vs. landline

  • Use of symbols like +, -, (, ) or spaces


Why Learn Regular Expressions?


At first glance, regular expressions—or "regex," as they're affectionately known—can seem daunting, almost as if someone dropped a bowl of alphabet soup onto your keyboard. But don’t let the cryptic symbols fool you. For developers, regex is a powerful tool that’s well worth having in your toolkit.


Whether you're parsing server logs, pulling out relevant information from web pages, or cleaning up inconsistent data from an API, regex streamlines the process. Instead of writing lengthy loops and conditionals, a single concise regex pattern can match and extract exactly what you need.

Some practical use cases include:

  • Validating user inputs like emails, phone numbers, or passwords

  • Searching and replacing text in files

  • Analyzing or filtering log data for diagnostics

  • Extracting specific fields from unstructured data, such as scraping job postings for salary figures


In short, regular expressions help you handle complex data manipulation quickly and efficiently—skills that save time and reduce errors in real-world development tasks.


Phone Number Regex Pattern in JavaScript

Basic International Phone Number Regex:

/^\+?[0-9]{1,4}?[-.\s]?(\(?\d{1,5}?\)?)[-.\s]?\d{1,5}[-.\s]?\d{1,9}$/


Pattern Breakdown:

  • ^\+? → Optional + for country code

  • [0-9]{1,4}? → Country code digits

  • [-.\s]? → Optional separator

  • (\(?\d{1,5}?\)?) → Optional area code with/without parentheses

  • \d{1,5} → Prefix or number section

  • \d{1,9} → Final part of the number

  • $ → End of string


When you're building forms to collect phone numbers—whether for contact, SMS notifications, or user profiles—users might enter numbers in a huge variety of ways. For example, all of the following are valid US-based numbers:

  • +1-202-555-0123

  • +44 20 7946 0958

  • (123) 456-7890


And that's just the tip of the iceberg! There are even more valid combinations out there, each with its own mix of separators, country codes, and spacing. Validating every possible variation by hand is a headache—but regex makes it simple and efficient to handle all these formats at once.



Code Example 1 – Basic Phone Number Validation


const phone = "+91-9876543210";
const pattern = /^\+?[0-9]{1,4}?[-.\s]?(\(?\d{1,5}?\)?)[-.\s]?\d{1,5}[-.\s]?\d{1,9}$/;

console.log(pattern.test(phone)); // true


Code Example 2 – Validate Multiple Numbers


const numbers = ["+1 800-123-4567", "1234567890", "(011) 2345 6789"];

const pattern = /^\+?[0-9]{1,4}?[-.\s]?(\(?\d{1,5}?\)?)[-.\s]?\d{1,5}[-.\s]?\d{1,9}$/;

numbers.forEach(num => {
  console.log(`${num}${pattern.test(num)}`);
});


Code Example 3 – Validate Input in a Web Form


<input type="text" id="phoneInput" placeholder="Enter your phone number" />
<p id="status"></p>

<script>
  const pattern = /^\+?[0-9]{1,4}?[-.\s]?(\(?\d{1,5}?\)?)[-.\s]?\d{1,5}[-.\s]?\d{1,9}$/;

  document.getElementById("phoneInput").addEventListener("input", function () {
    const isValid = pattern.test(this.value.trim());
    document.getElementById("status").textContent = isValid ? "✅ Valid Number" : "❌ Invalid Number";
  });
</script>


Metacharacters Used


  • ^ : Anchors the start of the string

  • $ : Anchors the end of the string

  • + : Matches one or more of the preceding token

  • ? : Makes the preceding token optional

  • () : Groups expressions

  • \d : Matches any digit (0–9)

  • [-.\s] : Matches a hyphen, period, or whitespace

  • \ : Escape character


Handling Optional Country Codes and Separators


Sometimes phone numbers start with a country code like , which may or may not be followed by a dash or space (e.g., 1-800-123-4567, 1 800 123 4567, or just 800-123-4567). To handle these, regex grouping and optional quantifiers come in handy.

For example, if you want your regex to optionally match a leading (the US country code), possibly followed by a space or dash, you can use:


^(1[ -]?)
  • groups the and the potential separator together.

  • The after the group makes the entire (and the separator after it) optional.


Combining this with the rest of a standard US phone number pattern, you get:

/^(1[ -]?)?\d{3}[ -]?\d{3}[ -]?\d{4}$

This matches:

  • 1-800-123-4567

  • 1 800 123 4567

  • 800-123-4567


Making Groups Optional in Regex


Sometimes, you need to make a whole sequence of elements optional, not just a single character. For example, in North American phone number formats, it's common to allow an optional leading (the country code), sometimes followed by a space or hyphen. To accomplish this in regex, group the elements you want to make optional with parentheses and then add a after the group.


Example:
To match numbers that may or may not start with , followed by an optional space or hyphen, you’d use:

^(1[ -]?)?\d{3}[ -]?\d{3}[ -]?\d{4}$

Here’s what’s happening:

  • The group matches an optional , possibly followed by a space or hyphen. The entire group is optional thanks to the trailing .

  • The typical 10-digit phone number format, with optional separators.

Grouping and making parts of your pattern optional is key for flexible validation, especially for formats like phone numbers that vary by region or user input habits.


US Number Example Pattern Breakdown:


  1. → Optionally start with (with optional space or dash)

  2. → Area code

  3. → Optional separator

  4. → Prefix

  5. → Optional separator

  6. → Line number

By combining these strategies, your regex can flexibly match a wide variety of international and domestic phone number formats, handling optional country codes, spaces, dashes, and parentheses commonly found in real-world data.


Common Valid US Phone Number Formats


This regex is flexible enough to match a wide variety of real-world phone number formats, especially those commonly seen in the US. Here are some examples of valid US-based numbers it will accept:

  • 202-515-5555

  • 202 515 5555

  • (202)515 5555

  • 1 202 515 5555

  • 2025155555

  • 1-202-515-5555

  • 1202-515-5555

  • and more

Whether the number uses spaces, dashes, or parentheses, or includes a leading country code, this pattern adapts to most practical scenarios you'll encounter in web forms and user input.


Pro Tips


  • Always .trim() input to remove accidental whitespace from the user.

  • If you only need Indian mobile numbers, simplify the regex to: /^[6-9]\d{9}$/

  • Use more specific patterns per country when validating local numbers.

  • Avoid allowing any character except digits, +, -, (, ), and spaces.

  • For global apps, test with a variety of international formats.

  • Consider integrating with libraries like libphonenumber for deeper validation (e.g., carrier detection, real-time feedback).


Use Cases


  • Signup Forms: Instantly validate user-entered phone numbers.

  • CRM Systems: Filter and clean phone numbers before adding to the database.

  • Marketing Automation: Ensure accurate numbers before sending SMS campaigns.

  • Booking Apps: Avoid invalid user entries in forms and call back flows.


While phone and email validation are probably the most common places you'll use regex, its real power shines through in a variety of situations. As you start coding more and more, regex becomes an indispensable tool—not just for form validation, but for extracting data from server logs, cleaning up messy JSON from API calls, and wrangling unstructured text in countless real-world scenarios. Think of it as your Swiss Army knife for data cleanup, pattern finding, and automation.


Combine with These Tools



Whether you're tidying up user input or pulling structured information out of a tangle of raw data, regex is a skill that pays off across the board—especially when paired with the right supporting tools.


Where can I find a good free tutorial to learn regex thoroughly (e.g., RegexOne)?


If you're looking to sharpen your regex skills, there’s a fantastic resource called RegexOne. It offers step-by-step lessons and interactive exercises that take you from the basics all the way to more advanced patterns. Working through their tutorials is an excellent way to get hands-on practice and build confidence with regular expressions—highly recommended if you want to really master the concepts.

Frequently asked questions

Can this regex validate all global formats?×
It covers many formats, but specific countries may require custom patterns.
Does it verify if the number is active?+
Why are parentheses allowed?+
Is the plus sign (+) mandatory?+
What’s the best way to clean user input before regex?+