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


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


This allows formats like:

  • +1-202-555-0123

  • +44 20 7946 0958

  • (123) 456-7890


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


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.


Combine with These Tools


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?+