Aligning users with formatted and unformatted phone numbers (from contacts, internationally)

This is what we have:

Users in the database, including the phone_number field. When users create their account, they must enter their phone number. We can request specific formatting and we do:

+ [countrycode] [phone number]

      

The user selects the country code and adds the required numbers. We assume he is entering it correctly.

Now he is in the database, and we are trying to match OTHER users with the numbers found in his contacts. Here we have a problem:

It can save its contacts with different number formatting (national, international, with +, without +). I'll take the Belgian number as an example to make my point clearer:

+32 495 12 34 56 (international)
0032 495 12 34 56 (international)
0 495 12 34 56 (national)

      

I assume most users will keep their contacts with national formatting and this is the problem.

Our application needs international formatting, and we need to compare the database users with contacts (this is part of our "application distribution" plan) so that the user chooses how he can invite the contact or not. and we don't want to show people who don't have an app (by the way).

How can I know if the phone number is formatted internationnaly or nationally, and either way, how can I transform it to be international?

      

If there is a +, I can assume it is correct, so I have no problem. If not, what can I do? I thought about this, but I'm not even sure:

  • Any number starting with 00, 100% international? And I just change 00 to + and okay?
  • Any number that doesn't match the other two critics I ...? I do not know.

Maybe get every number format in the world and use an incredibly long switch statement to convert it to the correct international format? I doubt this is the correct answer.

Anyway, any clue / advise / solution is definitely welcome!

Note. I am using Parse.com for data storage, if that helps.

+3


source to share


2 answers


Maybe libphonenumber is what you are looking for: https://code.google.com/p/libphonenumber/



+2


source


The correct way to do this is to check the format before saving the data. Parse.com has a "beforesave" feature that you can search and find examples. Then you should check the formatting at this point. I don't know any code that does this check in cloudy code, but I use Lua, for example, which has a pre-built function to get the character length of a string, or search for a specific character in a string. If you want to check on parse.com or on your device, there must be a function that can check the length of a string or a specific character.



0


source







All Articles