Question

My task is to take a phone number and determine what the numerical country code is (for example, 1 for Canada/US)

I have only started looking at libphonenumber for my phone number parsing needs. I tried the demo, and it explicitly requires me to enter a region code (eg: CA/US), so that doesn't allow me to determine whether it will do what I need without exploring the source code.

I have done some preliminary research on phone numbers only within North America, but the task requires me to consider phone numbers from around the world, which makes it much more difficult.

  1. Given only a non-normalized phone number, is it possible to determine what the country code is?
  2. Given an area code and the phone number, is it possible to determine what the country code is?

If 1 and 2 are not possible, what is the minimum information that I need to determine the country code for a particular phone number?

Was it helpful?

Solution

For my understanding on the issue, you want to determine the country code from the local number WITHOUT country code.

Well, country code is used to determine the country so that the phone caller does not have to make magics to enter to a specific country. Then inside the borders of the specific country the flow goes to the next round: area code determines the area. Then the local id tells the id.

I am from Finland and I have studied the Finnish number system. I can tell, that the amount of numbers on local id even vary a lot, there are several lengths of area codes and like I commented with links on your question, the international country codes are a mess itself already.

So, determining the international id from a FORMATTED number is trick #1. It is even not straight forward. Then each country determines areas by them selves (it belong to local regulation) and even local areas have their own systems e.g. depending how many subscribers are on specific area (and that may vary after the area codes are fixed long time ago) and how big number space is needed.

So, if you learn the number formatting system of one country, you can only guess that if number fits that it may belong to that country. Imagine a equal sized and equally divided neighbor country, are there same rules? Maybe there are: local regulations are not bound inclusively nor exclusively to each others. They are locally determined and nobody keeps marks about the rules, but only about country codes.

In short, answer to 1 and 2 is negative, not possible. Same applies both.

Then to tell what is bare minimum for finding a country code from a number, it is that the number has a country code included. Common way to force it in is to provide a list for selection having the country codes with appropriate flags before the actual [area][local id] section that is the second field ( they can be together, if you want, your choice). The thing why you see such solutions in many places is that it is The Only Way to do it meaningfully.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top