Validating international phone numbers

You'll want to parse the first few digits to determine what the country code is, then act differently based on the country.

The international accounts for an optional initial ' ' and country code. Valid matches would be: You'll have a hard time dealing with international numbers with a single/simple regex, see this post on the difficulties of international (and even north american) phone numbers.

sometimes the answer to a problem is to approach it differently. The following regex will catch widely used number and character combinations in a variety of global phone number formats: Positive: 42 555.123.4567 1-(800)-123-4567 567 7(926)1234567 (926) 1234567 792612345567 9261234567 1234567 123-4567 123-89-567 469 123 45 67 89261234567 8 (926) 1234567 926.123.4567 415-555-1234 650-555-2345 (416)555-3456 2 4035555678 1 4 Negative: 926 3 4 8 800 600-APPLE Original source: I believe the Number:: Phone:: US and Regexp:: Common (particularly the source of Regexp:: Common:: URI:: RFC2806) Perl modules could help.

The 555 prefix is still special, but only a small range of numbers are guaranteed to terminate without connection 555-0100 to 555-0199. This is good and all, but it doesn't validate what was entered was actually a phone number. Even if you could write a big, hairy validator that would allow all the different legitimate formats, it would end up allowing pretty much anything even remotely resembling a phone number in the first place.

For example, what if the user doesn't enter the requisite 10 digits? In my opinion, the most elegant solution is to validate a minimum length, nothing more.

Rather than try to solve all that, take the input and "pre-strip" it of all formatting fluff until you have just the "number". How are extensions (e.g., 111-222-3333 x 44444) going to be handled? So far, it's been working with everything they've thrown at it, but if errors come up, I'll update this answer.

Doing this solves 2 problems - testing the result is now easy and you can now ensure that values rendered back out for display can all be formatted consistently. Regex: Here's a wonderful pattern that most closely matched the validation that I needed to achieve.

Search for validating international phone numbers:

validating international phone numbers-70

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “validating international phone numbers”