Powerful Regex for Validating International Phone Numbers
Phone number validation can be tricky because of the varying formats across countries. Luckily, a powerful regular expression (regex) can help validate most international phone numbers. This regex pattern ensures that the number follows common international dialing formats, including country codes:
/(\+|00)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/
Breakdown of the Regex:
- (+|00): Matches either a plus sign
+
or00
as the international dialing prefix. - Country Code Validation: The pattern follows with groups like
9[976]
,8[987530]
, etc., which capture valid country code formats. - Up to 14 Digits: It ensures that the rest of the phone number is between 1 to 14 digits, which is common for international phone numbers.
Benefits of This Regex:
- Supports multiple country formats: The regex accounts for various country codes, making it suitable for global applications.
- Internationally recognized: It aligns with ITU-T E.164, a global standard for telephone numbering plans.
Use Case:
If you’re building a global web application, validating phone numbers is crucial for data integrity. This regex can be integrated into any programming language that supports regular expressions, such as JavaScript, Python, or PHP, to ensure that users input valid phone numbers in a standardized format.
Example in JavaScript:
Here’s how you can use this regex to validate phone numbers in JavaScript:
function validatePhoneNumber(phoneNumber) {
const phoneRegex = /(\+|00)(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/;
return phoneRegex.test(phoneNumber);
}
console.log(validatePhoneNumber('+1234567890')); // Example usage
This validation pattern provides flexibility and reliability when dealing with phone numbers from different countries.