Programming and Coding
Validating phone numbers with libphonenumber library in C#
To facilitate .NET developers to validate phone number in C#, Tom Clegg has created a port of libphonenumber to C#, libphonenumber-csharp. It's available as a NuGet package, which makes it easy to add to any project.
You can install it throught Visual Studio by right cliking on the project and choosing Manage Nuget pakcages...search phonenumber:
Or you can enter the following command in a Package Manager Console window, substituting the current version information as appropriate:
PM> Install-Package libphonenumber-csharp -Version 8.11.1
Or enter the following .NET command line in a PowerShell window. Be sure the current directory is the project directory:
dotnet add package libphonenumber-csharp --version 8.11.1
The following example shows you how to use libphonenumber to validate a phone number in Canada:
string inputPhoneNumber = "519888888";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
PhoneNumber queryPhoneNumber = phoneUtil.Parse(inputPhoneNumber, "CA");
resultPhoneNumber = queryPhoneNumber.NationalNumber.ToString();
resultPhoneNumber = string.Empty;
© 2020 Digcode.com. All rights reserved.