How to validate phone numbers using regex?
Validating phone numbers with a regular expression can vary widely depending on the country, format, and specific rules (e.g., whether you allow country codes, extensions, etc.). There is no one-size-fits-all regex for every phone number in the world. However, you can write a practical or region-specific regex that catches most typical formats for your use case. Below are a few common approaches.
1. A Simple, Generic “10-Digit” US Phone Number
If you only need to ensure the user enters a 10-digit North American number (like 555-123-4567 or (555) 123-4567), you might do:
^(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$
- Explanation:
- ^(?:\(\d{3}\)|\d{3})allows either- (555)or- 555.
- [-.\s]?optionally matches a dash, dot, or whitespace.
- \d{3}matches the next 3 digits.
- [-.\s]?optionally matches a separator again.
- \d{4}matches the final 4 digits.
- $ensures we’re at the end of the string.
 
This handles parentheses around the area code, optional separators, etc. Caveat: This doesn’t account for country codes (+1), extensions, or more exotic formats.
2. Adding Support for a Leading +1
If you want to optionally allow +1 for US numbers:
^\+?1?\s?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$
- \+?1?: Zero or one- +sign, zero or one “1” digit.
- \s?: Optional space after the country code.
- The rest is similar to the simple 10-digit pattern above.
3. Completely Generic “International” Phone Numbers
International standards like E.164 require up to 15 digits, often prefixed by a plus sign. A simplified approach to allow international numbers might be:
^\+?\d{1,15}$
- ^\+?: Optional “+” sign at the start.
- \d{1,15}$: From 1 to 15 digits total.
This is extremely permissive—it ensures only digits (and an optional leading plus) but doesn’t handle country-specific lengths or formatting details like spaces or hyphens. You might allow optional separators:
^\+?\d[\d\s.-]{0,14}\d$
- Ensures the first character after +is a digit, then allows up to 14 more characters which can be digits, spaces, dots, or hyphens.
- This is still quite permissive and might allow weird spacing or multiple hyphens in a row.
4. Country-Specific Patterns
Many countries have unique phone formats. For example, a simple UK pattern can be different from a French pattern, which differs from an Indian pattern, etc. If you know your target audience is mostly from one country or region, it’s best to craft a pattern specifically for that region’s conventions (including area codes, trunk prefixes, etc.).
5. Best Practices
- Keep It Simple
- Overly strict patterns can reject legitimate phone numbers (e.g., missing or extra parentheses, spacing in unusual places, etc.).
 
- Consider a Library
- If you need robust international validation, use libraries like Google’s libphonenumber or language-specific wrappers. They parse and normalize phone numbers for you.
 
- Post-Process
- In many cases, you only need to store digits and possibly a country code. You can strip out non-digit characters and then do simpler numeric checks (e.g., “must be 10 digits if local, up to 15 if international”).
 
- Focus on Use Case
- If you just want a phone number to “look okay,” a simple pattern is enough. If you need official, validated numbers, consider a more advanced approach (library, actual phone call/SMS verification, etc.).
 
Example in JavaScript
function isValidUsPhoneNumber(phone) { // Very simple US-based pattern const pattern = /^\+?1?\s?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}$/; return pattern.test(phone); } console.log(isValidUsPhoneNumber('555-123-4567')); // true console.log(isValidUsPhoneNumber('(555) 123-4567')); // true console.log(isValidUsPhoneNumber('+1 (555) 123-4567')); // true console.log(isValidUsPhoneNumber('12345')); // false
Final Thoughts
Phone numbers vary enormously in format and requirements worldwide. A single “one-size-fits-all” regex either becomes too permissive or unmanageably large. Use a practical pattern for your region or user base, or consider an international library like libphonenumber for more robust validation.
Bonus: Level Up Your Regex & Coding Skills
Regular expressions are an essential tool, but so is a strong JavaScript and system design foundation. For deeper learning, check out these DesignGurus.io courses:
- 
Grokking JavaScript Fundamentals 
 Build a solid JS foundation—closures, prototypes, async/await, and more.
- 
Grokking the Coding Interview: Patterns for Coding Questions 
 Master common coding patterns for problem solving in interviews and day-to-day tasks.
Looking for personalized feedback? Consider their Coding Mock Interview or System Design Mock Interview with ex-FAANG engineers. Also, explore free tutorials on the DesignGurus.io YouTube channel.
Bottom line: Keep your phone validation scope realistic and practical, especially if users come from multiple regions. When in doubt, use a library that’s frequently updated to handle all edge cases.