Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone number normalization: Any pre-existing libraries?

I have a system which is using phone numbers as unique identifiers. For this reason, I want to format all phone numbers as they come in using a normalized format. Because I have no control over my source data, I need to parse out these numbers myself and format them before adding them to my DB.

I'm about to write a parser that can read phone numbers in and output a normalized phone format, but before I do I was wondering if anyone knew of any pre-existing libraries I could use to format phone numbers.

If there are no pre-existing libraries out there, what things should I be keeping in mind when creating this feature that may not be obvious?

Although my system is only dealing with US numbers right now, I plan to try to include support for international numbers just in case since there is a chance it will be needed.

Edit I forgot to mention I'm using C#.NET 2.0.

like image 344
Dan Herbert Avatar asked Nov 03 '08 15:11

Dan Herbert


People also ask

What is phone number normalization?

Phone number normalization is used to translate a phone number into a standard, or normal, form. If numbers are not normalized, it is difficult to compare two phone numbers to see if they are the same. By changing all numbers into a normal form, Streem Center can check and handle them efficiently.

How do I store my phone number with country code?

Open your phone's address book. When adding the contact's phone number, start by entering a plus sign (+). Enter the country code, followed by the full phone number. Note: A country code is a numerical prefix that must be entered before the full national phone number to make a call to another country.


3 Answers

You could use libphonenumber from Google. Here's a blog post:

http://blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber

Parsing numbers is as easy as installing the NuGet package and then doing this:

var util = PhoneNumberUtil.GetInstance();
var number = util.Parse("555-555-5555", "US");

You can then format the number like this:

util.Format(number, PhoneNumberFormat.E164);

libphonenumber supports several formats other than E.164.

like image 132
friism Avatar answered Oct 07 '22 22:10

friism


I'm currently involved in the OpenMoko project, which is developing a completely open source cell phone (including hardware). There has been a lot of trouble around normalizing phone numbers. I don't know if anyone has come up with a good solution yet. The biggest problem seems to be with US phone numbers, since sometimes they come in with a 1 on the front and sometimes not. Depending on what you have stored in your contacts list, it may or may not display the caller ID info correctly. I'd recommend stripping off the 1 on the phone number (though I'd expect most people wouldn't enter it in the first place). You may also need to look for a plus sign or country code on the front of international numbers.

You can check around the OpenMoko website, mailing list, and source control to see if they've solved this bug yet.

like image 32
rmeador Avatar answered Oct 07 '22 21:10

rmeador


perl and rails examples


http://validates-as-phone.googlecode.com/svn/trunk/README

http://www.perlmonks.org/?node_id=159645

like image 39
Gene T Avatar answered Oct 07 '22 22:10

Gene T