Is there an easy way to check whether a phone number entered by the user includes country code and to validate that the number is correct? I don't use any specific formats, the number itself must be only digits, no ('s, -'s and the like. Is such validation possible without asking user for a country? The trick is that I want to work with all numbers world-wide.
I guess it can't be done with regex (googled a bit and found lots of stuff but not for this problem). Is there any library for it? I'm using python.
Or maybe it would make more sense to enforce a format e.g. X-YYYYYYYY... where X would be a country code, or something like this?
Here is Google's library for dealing with phone numbers (it's in Java). However, as Google is a Python shop as well, I imagine they might have a Python version available somewhere or you may be able to extract what you need and translate it.
I studied this subject for a while. We have a huge database with numbers, no one knows are they with contry code or not. General answer is: it is impossible to find it out. We had some numbers that were valid US numbers without country code (1) and valid numbers of some other contries with area code. But for 99% cases you can check if length of number is 9 then there is no contry code. You should enforce users to enter contry code.
A number like 247 8000 could be:
Without further context it is impossible to tell.
The + symbol is vital in identifying the country code part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With