Formatting phone numbers based on country code

user2849406 picture user2849406 · Oct 24, 2013 · Viewed 13.3k times · Source

I have a database with phone numbers which I would like to format according to the country code of the phone number.

The numbers are currently stored in the following format:

+[country_code][number] (like +4512345678)

What I need is some kind of library for javascript, that based on the country code can format the number according to the official national format (http://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers). Below are some examples of how some of the numbers could be formatted

+4512345678 => +45 12 34 56 78
+12892708970 => (289) 270-8970

I have found this Java-library here https://code.google.com/p/libphonenumber/, which bascially solves the problem. However I would like a simple Javascript-solution, which they actually demonstrate on their page, but I cannot find any official downloads to the file file they use, making me wonder if a better solution exists?

Answer

SaudiD3mon picture SaudiD3mon · Oct 24, 2013

Use libphonenumber-js https://github.com/googlei18n/libphonenumber

It has all the required features.

Credit