Phone number normalization: Any pre-existing libraries?

Dan Herbert picture Dan Herbert · Nov 3, 2008 · Viewed 10k times · Source

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.

Answer

friism picture friism · Feb 4, 2012

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.