I want to get the country code of user's phone number, he may have entered his/her phone without country code (11-digit).If it is possible then please help me out.
Asked
Active
Viewed 2,602 times
1
-
You have the power https://en.wikipedia.org/wiki/List_of_country_calling_codes#Alphabetical_listing_by_country_or_region – TheGeneral Jul 01 '20 at 09:01
-
Or if you want to use Googles phonenumber lib `PhoneNumber numberProto = phoneUtil.parse(phone, ""); int countryCode = numberProto.getCountryCode();` – TheGeneral Jul 01 '20 at 09:03
1 Answers
1
There's a great library called libphonenumber
(C# port: https://github.com/twcclegg/libphonenumber-csharp) which does convert phone numbers into valid E.164 numbers. However, you need to assume the number's origin country to correctly parse them.
public string ParsePhoneNumber(string number)
{
var phoneNumberUtil = PhoneNumbers.PhoneNumberUtil.GetInstance();
return phoneNumberUtil.Parse(nationalPhoneNumber, "DE");
}

Tim
- 43
- 2
- 8