9

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

Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72
  • Knowing the country of origin of at least ONE phone number (assuming you are always comparing two), is very helpful here...Then you can build a solid algorithm...I have done this a few times, let me know if you still want assistance :) – Grantly Dec 07 '17 at 18:48

3 Answers3

9

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.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

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.

RoadieRich
  • 6,330
  • 3
  • 35
  • 52
0

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....
}
rhughes
  • 9,257
  • 11
  • 59
  • 87