22

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
  • @Dan, I think you'd get a better answer if you tell us _which language_ you're using. :-) – Onorio Catenacci Nov 03 '08 at 15:37
  • I added the C#2.0 tag to this question. – Onorio Catenacci Nov 03 '08 at 15:42
  • @Joel, I don't want to start an edit war but I did tag the question with C#2.0 for an (I think) valid reason: http://stackoverflow.com/questions/247621/what-are-the-correct-version-numbers-for-c – Onorio Catenacci Nov 03 '08 at 16:00
  • @Dan did you find a library to normalize phone numbers? – marc.d Oct 28 '09 at 16:15
  • @marc.d No I did not. I ended up writing my own that suited my needs well enough. – Dan Herbert Oct 28 '09 at 17:33
  • @Dan could you please make your library public if possible? i would like to translate it to a javascript/jQuery solution. atm the only working example i found is written in perl, unfortunately my perl knowledge is near zero. – marc.d Oct 29 '09 at 08:39
  • @marc.d Unfortunately, most of the code was written under a project that was under an NDA. I can see what I can do though, since I can understand its usefulness. I'll update this question if I have anything I can make public. – Dan Herbert Oct 29 '09 at 13:25

5 Answers5

28

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.

Matt Bishop
  • 1,010
  • 7
  • 18
friism
  • 19,068
  • 5
  • 80
  • 116
5

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.

rmeador
  • 25,504
  • 18
  • 62
  • 103
2

perl and rails examples


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

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

Gene T
  • 5,156
  • 1
  • 24
  • 24
1

Just strip out any non-digits, possibly using a RegEx: [^\d]

The only exception might be if you want to handle extensions, to distinguish a number without an area code but with a 3 digit extension, or if you need to handle international numbers.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

What you need is list of all country codes and start matching your string first few characters against list of country codes to make sure it's correct then for the rest of the number, make sure it's all digits and of proper length which usually varies from 5-10 digits.

To achieve checking against country codes, install NGeoNames nuget which uses website www.geonames.org to get list of all country codes to use to match against them.

Korayem
  • 12,108
  • 5
  • 69
  • 56