libphonenumber-csharp 8.13.37

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package libphonenumber-csharp --version 8.13.37
NuGet\Install-Package libphonenumber-csharp -Version 8.13.37
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="libphonenumber-csharp" Version="8.13.37" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add libphonenumber-csharp --version 8.13.37
#r "nuget: libphonenumber-csharp, 8.13.37"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install libphonenumber-csharp as a Cake Addin
#addin nuget:?package=libphonenumber-csharp&version=8.13.37

// Install libphonenumber-csharp as a Cake Tool
#tool nuget:?package=libphonenumber-csharp&version=8.13.37

Build status codecov NuGet

C# port of Google's libphonenumber library.

The code was rewritten from the Java source mostly unchanged, please refer to the original documentation for sample code and API documentation.

The original Apache License 2.0 was preserved.

See this for details about the port.

Phone number metadata is updated in the Google repo approximately every two weeks. This library is automatically updated by a scheduled github action to include the latest metadata, usually within a day.

Installation

Run the following command to add this library to your project

dotnet add package libphonenumber-csharp

Available on NuGet as package libphonenumber-csharp.

Examples

Parsing a phone number

using PhoneNumbers;

var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var e164PhoneNumber = "+44 117 496 0123";
var nationalPhoneNumber = "2024561111";
var smsShortNumber = "83835";
var phoneNumber = phoneNumberUtil.Parse(e164PhoneNumber, null);
phoneNumber = phoneNumberUtil.Parse(nationalPhoneNumber, "US");
phoneNumber = phoneNumberUtil.Parse(smsShortNumber, "US");

Formatting a phone number

using PhoneNumbers;

var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var formattedPhoneNumber = phoneNumberUtil.Format(phoneNumber, PhoneNumberFormat.INTERNATIONAL);
var formattedPhoneNumberNational = phoneNumberUtil.Format(phoneNumber, PhoneNumberFormat.NATIONAL);

Console.WriteLine(formattedPhoneNumber.ToString()); // +1 415-666-7777
Console.WriteLine(formattedPhoneNumberNational.ToString()); // (415) 666-7777

Check if a phone number is valid

using PhoneNumbers;

var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var isValid = phoneNumberUtil.IsValidNumber(phoneNumber);

Console.WriteLine(isValid); // true

Get the type of a phone number

using PhoneNumbers;

var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", "US");
var numberType = phoneNumberUtil.GetNumberType(phoneNumber);

Console.WriteLine(numberType); // PhoneNumberType.FIXED_LINE_OR_MOBILE

See PhoneNumberType.cs for the various possible types of phone numbers

Get the region code for a phone number

using PhoneNumbers;

var phoneNumberUtil = PhoneNumberUtil.GetInstance();
var phoneNumber = phoneNumberUtil.Parse("+14156667777", null);
var regionCode = phoneNumberUtil.GetRegionCodeForNumber(phoneNumber);

Console.WriteLine(regionCode); // US

Features

  • Parsing/formatting/validating phone numbers for all countries/regions of the world.
  • GetNumberType - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
  • IsNumberMatch - gets a confidence level on whether two numbers could be the same.
  • GetExampleNumber/GetExampleNumberByType - provides valid example numbers for 218 countries/regions, with the option of specifying which type of example phone number is needed.
  • IsPossibleNumber - quickly guessing whether a number is a possible phone number by using only the length information, much faster than a full validation.
  • AsYouTypeFormatter - formats phone numbers on-the-fly when users enter each digit.
  • FindNumbers - finds numbers in text input

See PhoneNumberUtil.cs for the various methods and properties available.

ToDo

  • port read / write source xml data to binary for better performance and smaller .nupkg size (WIP)
  • update / add / port new unit tests and logging from java source

How to unfold automatic generated files

  • Install Jetbrains - Resharper for Visual Studio
  • File by file, right click and "Cleanup code"
  • Check the unfolded file

Running tests locally

To run tests locally, you will need a zip version of the geocoding.zip file stored in the resources folder and testgeocoding.zip file stored in the resources/test folder.

On linux, you can run the following commands to generate the zip accordingly

(cd resources/geocoding; zip -r ../../resources/geocoding.zip *)
(cd resources/test/geocoding; zip -r ../../../resources/test/testgeocoding.zip *)

For windows, you can use the following powershell script

Compress-Archive -Path "resources\geocoding\*" -DestinationPath "resources\geocoding.zip"
Compress-Archive -Path "resources\test\geocoding\*" -DestinationPath "resources\test\testgeocoding.zip"

Contributing

See CONTRIBUTING.md

Donations

Buy me a beer

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (101)

Showing the top 5 NuGet packages that depend on libphonenumber-csharp:

Package Downloads
N3O.Umbraco.Validation

TODO

GlobalPhone

Package Description

LineTen.Core

LineTen core interfaces and helper classes

Flynk.Common.Utils

Utilities for generic applications

horaios.de.Charis.Objects

Package Description

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on libphonenumber-csharp:

Repository Stars
OrchardCMS/OrchardCore
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
ONLYOFFICE/CommunityServer
Free open source office suite with business productivity tools: document and project management, CRM, mail aggregator.
notifo-io/notifo
Multi channel notification service for collaboration tools, e-commerce, news service and more.
shr670377723/CommunityServer-master
Version Downloads Last updated
8.13.39 21,416 6/15/2024
8.13.38 52,259 6/5/2024
8.13.37 137,787 5/17/2024
8.13.36 97,844 5/3/2024
8.13.35 134,941 4/19/2024
8.13.34 175,326 4/5/2024
8.13.33 135,899 3/25/2024
8.13.32 141,902 3/12/2024
8.13.31 162,466 2/25/2024
8.13.30 232,323 2/9/2024
8.13.29 203,489 1/26/2024
8.13.28 15,557 1/25/2024
8.13.27 474,219 12/21/2023
8.13.26 359,628 11/29/2023
8.13.25 258,791 11/20/2023
8.13.24 401,337 10/31/2023
8.13.23 248,625 10/17/2023
8.13.22 321,049 9/29/2023
8.13.21 162,201 9/20/2023
8.13.20 232,338 9/7/2023
8.13.19 249,518 8/22/2023
8.13.18 316,825 8/3/2023
8.13.17 122,112 7/27/2023
8.13.16 203,424 7/11/2023
8.13.15 1,589 7/11/2023
8.13.14 571,765 6/15/2023
8.13.13 1,628 6/15/2023
8.13.12 1,622 6/15/2023
8.13.11 903,404 4/27/2023
8.13.10 98,840 4/20/2023
8.13.9 178,475 4/10/2023
8.13.8 168,223 3/27/2023
8.13.7 593,818 3/3/2023
8.13.6 504,110 2/10/2023
8.13.5 230,242 1/29/2023
8.13.4 355,671 1/9/2023
8.13.3 356,306 12/22/2022
8.13.2 552,246 12/8/2022
8.13.1 234,050 11/28/2022
8.13.0 496,638 11/8/2022
8.12.57 673,947 10/14/2022
8.12.56 711,258 9/23/2022
8.12.55 443,395 9/9/2022
8.12.54 387,833 8/22/2022
8.12.53 427,749 8/8/2022
8.12.52 441,309 7/19/2022
8.12.51.1 145,171 7/12/2022
8.12.50 758,587 6/13/2022
8.12.49 340,261 6/1/2022
8.12.48 291,685 5/18/2022
8.12.47 6,133 5/18/2022
8.12.46 766,071 4/15/2022
8.12.45 1,072,114 3/14/2022
8.12.44 100,521 3/9/2022
8.12.43 445,896 2/14/2022
8.12.42 1,927 2/14/2022
8.12.41 1,933 2/14/2022
8.12.40 2,023 2/14/2022
8.12.39 1,999 2/14/2022
8.12.38 1,585,089 12/15/2021
8.12.37 2,422 12/15/2021
8.12.36 2,316 12/15/2021
8.12.35 2,111 12/15/2021
8.12.34 1,631,908 10/7/2021
8.12.33 382,805 9/23/2021
8.12.32 212,288 9/11/2021
8.12.31 2,454 9/11/2021
8.12.30 284,086 8/18/2021
8.12.29 2,216 8/18/2021
8.12.28 434,029 7/23/2021
8.12.27 249,358 7/8/2021
8.12.26 133,266 6/25/2021
8.12.25 131,754 6/16/2021
8.12.24 789,531 5/31/2021
8.12.23 233,526 5/13/2021
8.12.22 319,507 4/30/2021
8.12.21 334,768 4/8/2021
8.12.19 591,068 3/2/2021
8.12.18 451,573 2/10/2021
8.12.17 255,279 1/27/2021
8.12.16 144,046 1/15/2021
8.12.15 492,753 12/18/2020
8.12.14 307,198 12/4/2020
8.12.13 498,580 11/18/2020
8.12.11 1,052,481 10/9/2020
8.12.10 182,863 9/29/2020
8.12.9 398,376 9/2/2020
8.12.8 432,060 8/17/2020
8.12.7 487,498 7/21/2020
8.12.6 190,591 7/6/2020
8.12.5 2,575 7/6/2020
8.12.4 501,390 5/21/2020
8.12.3 190,522 5/8/2020
8.12.2 225,769 4/23/2020
8.12.1 487,321 3/31/2020
8.12.0 282,423 3/20/2020
8.11.5 298,271 3/3/2020
8.11.4 286,450 2/13/2020
8.11.3 277,005 2/3/2020
8.11.2 791,702 1/16/2020
8.11.1 379,390 12/12/2019
8.11.0 114,466 11/29/2019
8.10.23 88,574 11/18/2019
8.10.22 54,839 11/14/2019
8.10.21 403,476 10/17/2019
8.10.20 180,534 10/3/2019
8.10.19 178,900 9/21/2019
8.10.18 42,215 9/17/2019
8.10.17 283,521 8/16/2019
8.10.16 87,691 8/5/2019
8.10.15 193,804 7/18/2019
8.10.14 191,813 7/1/2019
8.10.13 432,242 5/31/2019
8.10.12 260,376 5/15/2019
8.10.11 82,509 5/7/2019
8.10.10 130,808 4/17/2019
8.10.9 251,325 4/5/2019
8.10.8 110,278 3/23/2019
8.10.7 146,952 3/11/2019
8.10.6 139,646 2/22/2019
8.10.5 454,175 2/8/2019
8.10.4 182,763 1/25/2019
8.10.3 127,391 1/11/2019
8.10.2 346,949 12/7/2018
8.10.1 64,557 11/29/2018
8.10.0 33,087 11/29/2018
8.9.16 403,339 10/19/2018
8.9.15 119,026 10/6/2018
8.9.14 90,000 9/21/2018
8.9.13 116,218 9/7/2018
8.9.12 225,051 8/25/2018
8.9.11 77,257 8/18/2018
8.9.10 302,332 7/12/2018
8.9.9 70,165 6/29/2018
8.9.8 70,579 6/15/2018
8.9.7 79,532 5/30/2018
8.9.6 123,025 5/16/2018
8.9.5 36,676 5/4/2018
8.9.4 101,493 4/17/2018
8.9.3 39,125 4/5/2018
8.9.2 69,647 3/19/2018
8.9.1 33,329 3/8/2018
8.9.0 49,050 2/22/2018
8.8.11 60,611 2/8/2018
8.8.10 36,501 1/25/2018
8.8.9 200,576 1/10/2018
8.8.8 147,800 12/8/2017
8.8.7 30,506 12/1/2017
8.8.6 66,346 11/17/2017
8.8.5 137,822 10/31/2017
8.8.4 134,255 10/17/2017
8.8.3 61,533 10/6/2017
8.8.2 46,256 9/22/2017
8.8.1 46,733 9/8/2017
8.8.0 96,319 8/22/2017
8.7.1 40,796 8/3/2017
8.7.0.1 54,487 7/21/2017
8.6.0.1 4,449 7/18/2017
8.6.0 40,249 7/10/2017
8.3.1.2 94,803 6/24/2017
8.3.1.1 126,216 3/13/2017
8.3.1 20,210 3/4/2017
7.7.4 321,847 11/13/2016
7.5.1 402,412 8/6/2016
7.2.5 413,193 2/11/2016
7.0.11 147,021 10/5/2015
7.0.9 79,844 8/16/2015
7.0.5.5 78,220 5/27/2015
7.0.5.4 3,145 5/23/2015
7.0.5.3 17,598 4/28/2015
7.0.5.1 2,953 4/27/2015
7.0.5 3,196 4/25/2015
4.10.0.62 282,776 8/21/2012
4.10.0.61 3,083 8/9/2012
4.10.0.59 3,255 7/27/2012
4.10.0.55 3,235 7/19/2012
4.9.0.52 3,134 7/13/2012
4.9.0.43 3,030 7/10/2012
4.9.0.42 3,148 7/9/2012
4.8.0.35 3,211 6/22/2012
4.8.0.33 3,992 6/16/2012
4.8.0.31 3,153 6/7/2012
4.8.0.30 3,704 5/31/2012
4.8.0.24 3,583 5/9/2012
4.7.0.21 4,414 4/28/2012
4.7.0.19 3,137 4/25/2012
4.6.0.16 3,461 3/30/2012
4.6.0.13 12,342 2/10/2012
4.5.0.9 6,575 1/30/2012
4.5.0.6 3,228 1/21/2012
4.5.0.4 4,099 1/20/2012
4.4.0.1 29,069 1/10/2012
3.8.0.400 3,283 11/28/2011
3.8.0.399 3,237 11/27/2011
3.8.0.394 3,266 11/24/2011
3.8.0.342 3,344 9/17/2011
3.7.1.342 3,261 9/17/2011
3.7.1.312 3,208 8/5/2011
3.7.1.306 8,538 8/5/2011