Reputation: 7265
I have been using googles libphonenumber to perform validation of US phone numbers. Now I am needing international support for all countries. Is it possible to reverse lookup a phone numbers country code using libphonenumber?
Say the DB has saved UK number +448456779463. Is it possible to libphonenumber to detect if that number is UK? It appears I can only check the validity of the number provided I know its country of origin first. But what to do if you have numbers and don't know its country of origin?
Upvotes: 3
Views: 1490
Reputation: 876
You can do it using libphonenumber
.
Following is example in python
import phonenumbers
number = "+44XXXXXXXX"
obj = phonenumbers.parse(number)
# get line type
phonenumbers.number_type(obj)
# validate phone
phonenumbers.is_valid_number(obj)
# validate phone for region
phonenumbers.is_valid_number_for_region(obj, "GB")
# get country name
from phonenumbers.geocoder import country_name_for_number
country_name_for_number(obj, "en")
I think following is what you looking exactly,
from phonenumbers.geocoder import region_code_for_number
phonenumbers.is_valid_for_region(obj, region_code_for_number(obj))
Upvotes: 0
Reputation: 3811
Twilio's Lookup tool can likely be of some help here.
It will return a country code based upon number input alone and you can retrieve additional information about a phone via the API.
https://www.twilio.com/docs/api/lookups
[Disclosure: I work for Twilio]
Upvotes: 2