Phone number normalization: Any pre-existing libraries?

前端 未结 5 865
不知归路
不知归路 2021-02-05 07:35

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

相关标签:
5条回答
  • 2021-02-05 08:07

    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.

    0 讨论(0)
  • 2021-02-05 08:14

    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.

    0 讨论(0)
  • 2021-02-05 08:16

    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.

    0 讨论(0)
  • 2021-02-05 08:26

    perl and rails examples


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

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

    0 讨论(0)
  • 2021-02-05 08:34

    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.

    0 讨论(0)
提交回复
热议问题