Simple PHP International Phone Number Validator
Recently I needed to validate that (at least the format of) an international phone number was valid in PHP. I searched, and as usual, found nothing.
There is, of course, LibPhoneNumber that Google wrote and everyone has hailed as the best thing since sliced bread because Google made it. I mean, Google made it... so it must be amazing... right? *bzzt!* Wrong! Anyway, people have written PHP, Python, etc front ends to access it... but, not only is LibPhoneNumber written in Java *GAH!*, but it's also a huge
pain in the vagina to install. Plus, most web hosts won't let you install random crap on their servers -- especially crap written in Java.
For this reason, and because I don't like to conform, I've written my own simple international telephone number validator in PHP. It's simple PHP code that can either be include();'d
or you can just paste the function at the bottom of your script. [Kindly leave my credit intact if you go with the paste option.].
The function expects you to pass the country code and then the telephone number to it. It will check that the length of the phone number is valid for the country. It doesn't actually check that the phone number is dialable, or not disconnected, or anything like that. "2120000000" would come back as a valid number for the US or Canada or a handfull of island nations, even though it's clearly not a dialable number.
It's also best to strip out punctuation and such before sending the number. Eg: "(212) 679-9999" would be invalid. As would "216.619.7910". And, of course, don't include the country code (which you're already passing) in the number. Eg: "+1(800) 555-1212" would fail.
Download it here.
Just rename it from checknumber.php.txt to checknumber.php.
Alternatively: wget -o checknumber.php http://moo.net/code/checknumber.php.txt
checknumber.php - V 0.2
(C) 2014 James Nonnemaker (james[at]ustelcom[dot]net) - Feel free to redistribute/modify, but please give me credit.
Dialing plan info provided by: http://www.itu.int/dms_pub/itu-t/opb/sp/T-SP-E.164C-2011-PDF-E.pdf
Back to Code Help
| Moo! Networks
For the search engines: PHP, validate international phone number, validate international telephone number, verify telephone number, php phone number checker, libphonenumber alternative, libphonenumber for php alternative