Get Country code from a random international cell phone number

13,531 views
Skip to first unread message

Thomas Dittmar

unread,
May 6, 2013, 9:32:49 AM5/6/13
to libphonenum...@googlegroups.com
Hi folks,

I have a list of all sorts of cell phone number that could potentially come from everywhere in the world. The list is populated from vCards. The numbers can have the following format:

0402 884 441
(04) 1105-8408 

016094142000

Is this library able to determine the country code and the cell number from all cell phone numbers in the world and if yes what methods are the most appropriate.

Thank you in advance.

Cheers,

Thomas

Lara Rennie

unread,
May 6, 2013, 9:40:47 AM5/6/13
to libphonenum...@googlegroups.com
No, it cannot. A number may be valid in many different countries.

If the number starts with a +, there are no problems - you can just parse it (with any region code, including "ZZ" which is the unknown region), and afterwards you can ask what the region code is.

However, if it doesn't, you have to guess the region code to provide when parsing. After this, you could validate it to see if it is a valid number for that country, but even if it is, it doesn't guarantee it was originally for that country.


2013/5/6 Thomas Dittmar <dittmarc...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "libphonenumber-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libphonenumber-di...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Thomas Dittmar

unread,
May 6, 2013, 9:53:05 AM5/6/13
to libphonenum...@googlegroups.com
Hi Lara,

Thank you very much for the quick answer.

I sort of expected this answer because I gave this problem a try before and run into all sorts of problems.

Anyway, perhaps I should ask the user what countries are expected in the vCards before uploading them and just check for these countries.

Thanks again for the help.

Cheers,
Thomas


2013/5/6 Thomas Dittmar <dittmarc...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to libphonenumber-discuss+unsub...@googlegroups.com.

Thomas Dittmar

unread,
May 6, 2013, 8:47:52 PM5/6/13
to libphonenum...@googlegroups.com
Hi Lara,

I have only one more question.

I have implemented the PhoneNumberUtil and Phonenumber.PhoneNumber library and played around with the parser.

Although I used the following syntax:

PhoneNumber numberInfo = phoneUtil.parse(cellNumber, "AU");

the parser was able to pick up not only Australian cell phone numbers but also the German ones:

0402 884 441 = Country Code: 61 National Number: 402884441

+61416624855 = Country Code: 61 National Number: 416624855

(04) 1105-8408 = Country Code: 61 National Number: 411058408 

016094142000 = Country Code: 49 National Number: 16094142000


How is that possible?

Thomas

--
Best regards,
--------------------------------------------
D-IT
Dittmar Consulting

Thomas Dittmar
31 A Byron St
Elwood, Victoria, 3184
Australia

T:  03 9525 7410
M: 04 0407 0738
http://tinyurl.com/dittmarconsulting
--------------------------------------------

Lara Rennie

unread,
May 7, 2013, 3:15:36 AM5/7/13
to libphonenum...@googlegroups.com
Hi Thomas,

This is a bit confusing, because what I think you've said doesn't happen.

The first three cases look fine, but the last number, if you go to the demo (http://libphonenumber.appspot.com/) and enter "016094142000" and AU, you get:

country_code61
national_number 16094142000

Which is not the country code 49 you claim to get. Are you sure this is the full string, and that you are only parsing with AU and not also trying DE? Note that this is an invalid number for AU, so if you called isValidNumber on it it would have returned false.

Or have I misinterpreted somehow what you're doing?

Lara


2013/5/7 Thomas Dittmar <dittmarc...@gmail.com>

Thomas Dittmar

unread,
May 7, 2013, 9:31:09 AM5/7/13
to libphonenum...@googlegroups.com
Hi Lara,

As you said before dial codes can be valid in different countries. As it happens this applies to Germany and Australia too.

You are right I didn't validate the number before I printed them out. 

Now it works.

Again thank you very much Lara.

Hejter From UK

unread,
Jul 22, 2020, 7:09:27 PM7/22/20
to libphonenumber-discuss
hi i used the phone but i can get the code uf you cangive methe code i cant make a apple id

Aravind Penmetsa

unread,
Jul 23, 2020, 1:42:17 AM7/23/20
to libphonenum...@googlegroups.com
Hi,

If country code is not present in the number itself (like +61 in E.164 format), you need to pass on default_region;
otherwise the library will not know the region it belongs to. This is because the same numbers can be valid in different regions.
Maybe pass on the region based on your best guess like region of the source (like Mobile sim).

Thanks & Regards,
Aravind

--
You received this message because you are subscribed to the Google Groups "libphonenumber-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libphonenumber-di...@googlegroups.com.


--
Thanks & Regards,
Aravind
Reply all
Reply to author
Forward
0 new messages