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 .
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 trueor false w.r.t phone number region
- bool isValidRegion = phoneUtil.IsValidNumberForRegion(phoneNumber, countryCode);
-
- string region = phoneUtil.GetRegionCodeForNumber(phoneNumber);
-
- var numberType = phoneUtil.GetNumberType(phoneNumber);
-
- string phoneNumberType = numberType.ToString();
-
- if (!string.IsNullOrEmpty(phoneNumberType) && phoneNumberType == "MOBILE")
- {
- isMobile = true;
- }
-
- var originalNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.E164);
-
- 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"
- };
-
-
- }
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.
- string displayNumber = string.Empty;
-
- PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
- PhoneNumbers.PhoneNumber phoneNumber = phoneUtil.Parse(telephoneNumber, dialFrom);
-
- displayNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.NATIONAL);
-
-
- displayNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.INTERNATIONAL);
-
-
-
- displayNumber = phoneUtil.FormatOutOfCountryCallingNumber(phoneNumber, dialFrom);