Integrate Google's Libphonenumber In ASP.NET Core

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. 

After creating ASP.NET Core project using Visual Studio 2017 first we have to install ibphonenumber-csharp .

.NET Core

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 
  1.                   PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();    
  2.                 try    
  3.                 {    string telephoneNumber = "03336323900";
  4.                      string countryCode = "PK";
  5.                     PhoneNumbers.PhoneNumber phoneNumber = phoneUtil.Parse(telephoneNumber, countryCode);    
  6.     
  7.                     bool isMobile = false;    
  8.                     bool isValidNumber = phoneUtil.IsValidNumber(phoneNumber); // returns true for valid number    
  9.     
  10.                     // returns trueor false w.r.t phone number region  
  11.                     bool isValidRegion = phoneUtil.IsValidNumberForRegion(phoneNumber, countryCode);   
  12.     
  13.                     string region = phoneUtil.GetRegionCodeForNumber(phoneNumber); // GB, US , PK    
  14.     
  15.                     var numberType = phoneUtil.GetNumberType(phoneNumber); // Produces Mobile , FIXED_LINE    
  16.     
  17.                     string phoneNumberType = numberType.ToString();    
  18.     
  19.                     if (!string.IsNullOrEmpty(phoneNumberType) && phoneNumberType == "MOBILE")    
  20.                     {    
  21.                         isMobile = true;    
  22.                     }    
  23.     
  24.                     var originalNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.E164); // Produces "+923336323997"    
  25.     
  26.                     var data = new ValidatePhoneNumberModel    
  27.    &nbfontsp;                {    
  28.                         FormattedNumber = originalNumber,    
  29.                         IsMobile = isMobile,    
  30.                         IsValidNumber = isValidNumber,    
  31.                         IsValidNumberForRegion = isValidRegion,    
  32.                         Region = region    
  33.                     };    
  34.     
  35.                      returnResult = new GenericResponse<ValidatePhoneNumberModel>() { Data = data,  
  36.                      StatusCode = HttpStatusCode.OK, StatusMessage = "Success" };    
  37.     
  38.                 }    
  39.                 catch (NumberParseException ex)    
  40.                 {    
  41.     
  42.                     String errorMessage = "NumberParseException was thrown: " + ex.Message.ToString();    
  43.     
  44.     
  45.                     returnResult = new GenericResponse<ValidatePhoneNumberModel>()    
  46.                     {    
  47.                         Message = errorMessage,    
  48.                         StatusCode = HttpStatusCode.BadRequest,    
  49.                         StatusMessage = "Failed"    
  50.                     };    
  51.     
  52.     
  53.                 }    
Display number in different formats
 
As we know, when we have to dial a number outside the country we have to use the national dialing according to the region.
The formatting method of libphone library helps us to display a number in local, international and outside country format.
  1. string displayNumber = string.Empty;  
  2.   
  3. PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();  
  4. PhoneNumbers.PhoneNumber phoneNumber = phoneUtil.Parse(telephoneNumber, dialFrom);  
  5. // Produces 0333 6329900  
  6. displayNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.NATIONAL);  
  7.   
  8. // Produces International format: +923336323997  
  9. displayNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.INTERNATIONAL);

  10. //Out - of - country format from US: 011 92 333 6323900  
  11. //Out - of - country format from UK: 00 92 333 6323900  
  12.   
  13. displayNumber = phoneUtil.FormatOutOfCountryCallingNumber(phoneNumber, dialFrom);  
You can get sample project from GITHUB REPOSITORY.