Recently, I got a chance to work on a project in which we have to validate and parse phone numbers before storing them in the database and found Google's libphonenumber very effective for this purpose.
Let's discuss the libphonenumber features and its integration in ASP.NET Core C# project.
Let's discuss the libphonenumber features and its integration in ASP.NET Core C# project.
After creating ASP.NET Core project using Visual Studio 2017 first we have to install ibphonenumber-csharp .

QUICK EXAMPLE: PARSE and VALID Number
To integrate this library we start by creating the instance. Then, using that, we can parse and validate the number by providing a telephone number and country code in ISO2 formate which returns the phoneNumber object as validation result and contains different properties such as IsValidNumber, IsValidNumberForRegion, GetRegionCodeForNumber, GetNumberType, and Formate method
- PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
- try
- { string telephoneNumber = "03336323900";
- string countryCode = "PK";
- PhoneNumbers.PhoneNumber phoneNumber = phoneUtil.Parse(telephoneNumber, countryCode);
- bool isMobile = false;
- bool isValidNumber = phoneUtil.IsValidNumber(phoneNumber); // returns true for valid number
- // returns trueor false w.r.t phone number region
- bool isValidRegion = phoneUtil.IsValidNumberForRegion(phoneNumber, countryCode);
- string region = phoneUtil.GetRegionCodeForNumber(phoneNumber); // GB, US , PK
- var numberType = phoneUtil.GetNumberType(phoneNumber); // Produces Mobile , FIXED_LINE
- string phoneNumberType = numberType.ToString();
- if (!string.IsNullOrEmpty(phoneNumberType) && phoneNumberType == "MOBILE")
- {
- isMobile = true;
- }
- var originalNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.E164); // Produces "+923336323997"
- var data = new ValidatePhoneNumberModel
- &nbfontsp; {
- FormattedNumber = originalNumber,
- IsMobile = isMobile,
- IsValidNumber = isValidNumber,
- IsValidNumberForRegion = isValidRegion,
- Region = region
- };
- returnResult = new GenericResponse<ValidatePhoneNumberModel>() { Data = data,
- StatusCode = HttpStatusCode.OK, StatusMessage = "Success" };
- }
- catch (NumberParseException ex)
- {
- String errorMessage = "NumberParseException was thrown: " + ex.Message.ToString();
- returnResult = new GenericResponse<ValidatePhoneNumberModel>()
- {
- Message = errorMessage,
- StatusCode = HttpStatusCode.BadRequest,
- StatusMessage = "Failed"
- };
- }

Ammar HeidariPosted Feb 16, 2021, 7:50 PM
You can use this nuget package: https://www.nuget.org/packages/Arad.LibPhoneNumber Sample code: var IsViablePhoneNumber = PhoneNumberUtil.IsViablePhoneNumber("989123456789"); var MCC_MNC = PhoneNumberUtil.GetMCCMNC("989123456789"); var Operator = PhoneNumberUtil.GetOperator("989123456789"); var Brand = PhoneNumberUtil.GetBrand("989123456789"); var OperatorStatus = PhoneNumberUtil.GetOperatorStatus(232 ,10); var OperatorType = PhoneNumberUtil.GetOperatorType(232 ,10);
Shimmy WeitzhandlerPosted Mar 28, 2020, 6:04 PM
This one seems to be much more active: https://github.com/twcclegg/libphonenumber-csharp
Roger OspinaPosted Oct 18, 2019, 7:15 PM
Hello, thanks so much for your post. Do you think would it be possible to get the Code of the country only with the phone number ? For example, If I receive this phone number +57 301 5100000 then it'd answer ? "CO" (because 57 is the code for Colombia). Thanks again