libphonenumber-csharp 9.0.2

Prefix Reserved
dotnet add package libphonenumber-csharp --version 9.0.2
                    
NuGet\Install-Package libphonenumber-csharp -Version 9.0.2
                    
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="9.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="libphonenumber-csharp" Version="9.0.2" />
                    
Directory.Packages.props
<PackageReference Include="libphonenumber-csharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add libphonenumber-csharp --version 9.0.2
                    
#r "nuget: libphonenumber-csharp, 9.0.2"
                    
#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.
#addin nuget:?package=libphonenumber-csharp&version=9.0.2
                    
Install libphonenumber-csharp as a Cake Addin
#tool nuget:?package=libphonenumber-csharp&version=9.0.2
                    
Install libphonenumber-csharp as a Cake Tool

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.

Why keep libphonenumber-csharp up to date?

A lot of the functionality depends on updated metadata that is published by the google repository, see example here.

This means that if you don't keep the package up to date, methods like IsValidNumber will return false for newer numbers that rely on the updated metadata

Therefore, we recommend you keep this nuget package as up to date as possible using automated means (such as dependabot) as metadata changes published by the google repository is frequent, usually a few times a month.

For more information on metadata usage, please refer to the main repository faq

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 was computed.  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 was computed.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.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 (124)

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

Package Downloads
N3O.Umbraco.Validation

TODO

OrchardCore.Users

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. The Users module adds user management functionality.

OrchardCore.Application.Cms.Core.Targets

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with TheAdmin theme but without any front-end Themes.

TheTheme

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. The default Theme - Can be used as base theme for new themes.

OrchardCore.Application.Cms.Targets

Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework. Converts the application into a modular OrchardCore CMS application with following themes. - TheAdmin Theme - SafeMode Theme - TheAgency Theme - TheBlog Theme - TheComingSoon Theme - TheTheme theme

GitHub repositories (5)

Showing the top 5 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.
bitfoundation/bitplatform
Build all of your apps using what you already know and love ❤️
notifo-io/notifo
Multi channel notification service for collaboration tools, e-commerce, news service and more.
shr670377723/CommunityServer-master
Version Downloads Last updated
9.0.2 44,397 3/28/2025
9.0.1 25,384 3/23/2025
8.13.55 389,997 2/14/2025
8.13.54 172,661 1/31/2025
8.13.53 140,492 1/21/2025
8.13.52 533,841 12/13/2024
8.13.51 235,033 12/2/2024
8.13.50 405,333 11/16/2024
8.13.49 293,811 11/4/2024
8.13.48 153,326 10/23/2024
8.13.47 624,867 10/4/2024
8.13.46 246,644 9/25/2024
8.13.45 477,678 9/6/2024
8.13.44 309,616 8/26/2024
8.13.43 470,151 8/9/2024
8.13.42 252,595 7/26/2024
8.13.41 27,262 7/25/2024
8.13.40 591,464 7/3/2024
8.13.39 425,470 6/15/2024
8.13.38 222,552 6/5/2024
8.13.37 966,281 5/17/2024
8.13.36 288,675 5/3/2024
8.13.35 397,785 4/19/2024
8.13.34 405,478 4/5/2024
8.13.33 416,667 3/25/2024
8.13.32 399,063 3/12/2024
8.13.31 272,964 2/25/2024
8.13.30 414,581 2/9/2024
8.13.29 425,032 1/26/2024
8.13.28 23,906 1/25/2024
8.13.27 810,287 12/21/2023
8.13.26 617,997 11/29/2023
8.13.25 378,919 11/20/2023
8.13.24 698,183 10/31/2023
8.13.23 432,187 10/17/2023
8.13.22 443,926 9/29/2023
8.13.21 230,169 9/20/2023
8.13.20 360,010 9/7/2023
8.13.19 351,152 8/22/2023
8.13.18 423,668 8/3/2023
8.13.17 148,870 7/27/2023
8.13.16 243,194 7/11/2023
8.13.15 1,898 7/11/2023
8.13.14 730,598 6/15/2023
8.13.13 1,908 6/15/2023
8.13.12 1,919 6/15/2023
8.13.11 1,235,759 4/27/2023
8.13.10 131,859 4/20/2023
8.13.9 242,247 4/10/2023
8.13.8 205,712 3/27/2023
8.13.7 869,245 3/3/2023
8.13.6 577,799 2/10/2023
8.13.5 255,183 1/29/2023
8.13.4 416,513 1/9/2023
8.13.3 483,112 12/22/2022
8.13.2 719,533 12/8/2022
8.13.1 263,790 11/28/2022
8.13.0 569,815 11/8/2022
8.12.57 876,788 10/14/2022
8.12.56 852,567 9/23/2022
8.12.55 546,391 9/9/2022
8.12.54 456,750 8/22/2022
8.12.53 519,439 8/8/2022
8.12.52 514,739 7/19/2022
8.12.51.1 158,621 7/12/2022
8.12.50 871,378 6/13/2022
8.12.49 411,485 6/1/2022
8.12.48 324,711 5/18/2022
8.12.47 6,772 5/18/2022
8.12.46 866,765 4/15/2022
8.12.45 1,181,770 3/14/2022
8.12.44 105,675 3/9/2022
8.12.43 564,957 2/14/2022
8.12.42 2,023 2/14/2022
8.12.41 2,037 2/14/2022
8.12.40 2,130 2/14/2022
8.12.39 2,098 2/14/2022
8.12.38 1,829,076 12/15/2021
8.12.37 2,574 12/15/2021
8.12.36 2,455 12/15/2021
8.12.35 2,218 12/15/2021
8.12.34 1,933,757 10/7/2021
8.12.33 433,778 9/23/2021
8.12.32 236,770 9/11/2021
8.12.31 2,575 9/11/2021
8.12.30 310,621 8/18/2021
8.12.29 2,367 8/18/2021
8.12.28 494,829 7/23/2021
8.12.27 268,767 7/8/2021
8.12.26 171,244 6/25/2021
8.12.25 142,955 6/16/2021
8.12.24 871,327 5/31/2021
8.12.23 260,038 5/13/2021
8.12.22 430,728 4/30/2021
8.12.21 387,427 4/8/2021
8.12.19 633,555 3/2/2021
8.12.18 527,421 2/10/2021
8.12.17 267,655 1/27/2021
8.12.16 149,933 1/15/2021
8.12.15 524,676 12/18/2020
8.12.14 334,803 12/4/2020
8.12.13 519,294 11/18/2020
8.12.11 1,114,393 10/9/2020
8.12.10 195,593 9/29/2020
8.12.9 424,829 9/2/2020
8.12.8 473,535 8/17/2020
8.12.7 532,979 7/21/2020
8.12.6 197,059 7/6/2020
8.12.5 2,689 7/6/2020
8.12.4 542,909 5/21/2020
8.12.3 208,457 5/8/2020
8.12.2 238,578 4/23/2020
8.12.1 551,317 3/31/2020
8.12.0 293,553 3/20/2020
8.11.5 320,875 3/3/2020
8.11.4 306,855 2/13/2020
8.11.3 293,261 2/3/2020
8.11.2 879,920 1/16/2020
8.11.1 413,759 12/12/2019
8.11.0 132,300 11/29/2019
8.10.23 97,791 11/18/2019
8.10.22 55,863 11/14/2019
8.10.21 452,352 10/17/2019
8.10.20 203,617 10/3/2019
8.10.19 203,363 9/21/2019
8.10.18 48,342 9/17/2019
8.10.17 301,711 8/16/2019
8.10.16 94,655 8/5/2019
8.10.15 203,980 7/18/2019
8.10.14 231,337 7/1/2019
8.10.13 463,512 5/31/2019
8.10.12 273,247 5/15/2019
8.10.11 85,916 5/7/2019
8.10.10 137,539 4/17/2019
8.10.9 256,062 4/5/2019
8.10.8 116,362 3/23/2019
8.10.7 149,433 3/11/2019
8.10.6 152,454 2/22/2019
8.10.5 502,673 2/8/2019
8.10.4 201,346 1/25/2019
8.10.3 142,934 1/11/2019
8.10.2 381,485 12/7/2018
8.10.1 65,079 11/29/2018
8.10.0 39,810 11/29/2018
8.9.16 618,158 10/19/2018
8.9.15 121,985 10/6/2018
8.9.14 94,400 9/21/2018
8.9.13 117,630 9/7/2018
8.9.12 229,521 8/25/2018
8.9.11 103,057 8/18/2018
8.9.10 323,794 7/12/2018
8.9.9 86,593 6/29/2018
8.9.8 71,594 6/15/2018
8.9.7 88,707 5/30/2018
8.9.6 136,905 5/16/2018
8.9.5 38,921 5/4/2018
8.9.4 109,650 4/17/2018
8.9.3 41,534 4/5/2018
8.9.2 73,700 3/19/2018
8.9.1 35,061 3/8/2018
8.9.0 49,964 2/22/2018
8.8.11 64,799 2/8/2018
8.8.10 37,226 1/25/2018
8.8.9 228,246 1/10/2018
8.8.8 150,691 12/8/2017
8.8.7 30,793 12/1/2017
8.8.6 70,578 11/17/2017
8.8.5 183,385 10/31/2017
8.8.4 141,605 10/17/2017
8.8.3 66,424 10/6/2017
8.8.2 53,493 9/22/2017
8.8.1 48,221 9/8/2017
8.8.0 135,666 8/22/2017
8.7.1 46,081 8/3/2017
8.7.0.1 57,073 7/21/2017
8.6.0.1 4,821 7/18/2017
8.6.0 45,555 7/10/2017
8.3.1.2 100,719 6/24/2017
8.3.1.1 130,154 3/13/2017
8.3.1 21,575 3/4/2017
7.7.4 346,971 11/13/2016
7.5.1 433,587 8/6/2016
7.2.5 465,480 2/11/2016
7.0.11 156,507 10/5/2015
7.0.9 84,605 8/16/2015
7.0.5.5 80,350 5/27/2015
7.0.5.4 3,312 5/23/2015
7.0.5.3 18,918 4/28/2015
7.0.5.1 3,125 4/27/2015
7.0.5 3,386 4/25/2015
4.10.0.62 319,006 8/21/2012
4.10.0.61 3,252 8/9/2012
4.10.0.59 3,423 7/27/2012
4.10.0.55 3,419 7/19/2012
4.9.0.52 3,339 7/13/2012
4.9.0.43 3,220 7/10/2012
4.9.0.42 3,343 7/9/2012
4.8.0.35 4,295 6/22/2012
4.8.0.33 4,236 6/16/2012
4.8.0.31 3,334 6/7/2012
4.8.0.30 4,155 5/31/2012
4.8.0.24 3,762 5/9/2012
4.7.0.21 4,635 4/28/2012
4.7.0.19 3,332 4/25/2012
4.6.0.16 3,669 3/30/2012
4.6.0.13 14,046 2/10/2012
4.5.0.9 7,261 1/30/2012
4.5.0.6 3,409 1/21/2012
4.5.0.4 4,285 1/20/2012
4.4.0.1 32,034 1/10/2012
3.8.0.400 3,487 11/28/2011
3.8.0.399 3,415 11/27/2011
3.8.0.394 3,486 11/24/2011
3.8.0.342 3,540 9/17/2011
3.7.1.342 3,436 9/17/2011
3.7.1.312 3,392 8/5/2011
3.7.1.306 17,277 8/5/2011