Question

I am receiving phone numbers from a mobile device, format varies from international format or not.

Scenario (ZA formats just for the example) :

Registered numbers in database are always international format : +27827177982

Numbers I receive can vary e.g. +27827177982 = 27827177982 = 0827177982 - international prefix for number is +27

How do I match it to the international format even though I don't receive the international format???


Keep in mind :

I can't do conversion just for 1 region.

Is there a simple way of comparing mobile phone numbers for all regions?

Prefixes ranges in amount of chars reference : http://en.wikipedia.org/wiki/List_of_country_calling_codes


My ideas :

  • Compare the last 9 characters of the number, this will rule out the region prefix... but does every region only have '9 characters excluding the prefix'?

  • Loop through the database comparing the phone numbers a couple a times e.g. check for last 9 numbers - if no match - check for last 10 etc. (But can cause unwanted matches)


Any help would greatly be appreciated

Was it helpful?

Solution

You may want to look into using a library for this. For example, Google's libphonenumber library with a C# port being here. In particular, these two methods may be worth looking into (emphasis mine)

isNumberMatch - gets a confidence level on whether two numbers could be the same.

getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.

OTHER TIPS

In most countries (the US being a notable exception), non-international numbers start with a 0, so the solution will be something like:

  • If the country is on a list of exceptions, deal with it specially.
  • otherwise
    • If first character is 0, remove it and add international country code
    • Ensure first character is +.

There isn't any easy answer to this, because there are no internationally set rules no how phone numbers work.

How about something like this. This is written off the top of my head:

string sourcePhoneNumber = "....";
string phoneNumber = "....";

int baseRegionCountryCode = 44;

if (phoneNumber.StartsWith("0") && !phoneNumber.StartsWith("00"))
{
    phoneNumber = phoneNumber.SubString(0, 1);
    phoneNumber = String.Format("{0}{1}", baseRegionCountryCode, phoneNumber);
}
else if (phoneNumber.StartsWith("+"))
{
    phoneNumber = phoneNumber.Replace("+", "00");
}

if (sourcePhoneNumber == phoneNumber)
{
    // do something awesome....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top