[ACCEPTED]-Phone number normalization: Any pre-existing libraries?-phone-number

Accepted answer
Score: 28

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


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

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

You can then format 2 the number like this:

util.Format(number, PhoneNumberFormat.E164);

libphonenumber supports several formats 1 other than E.164.

Score: 5

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

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

Score: 1

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

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

Score: 0

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

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

More Related questions