Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if phone number entered by user includes country code?

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?

like image 461
PawelRoman Avatar asked Jun 26 '10 18:06

PawelRoman


3 Answers

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.

like image 65
Andy Avatar answered Sep 24 '22 22:09

Andy


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.

like image 39
Andrey Avatar answered Sep 22 '22 22:09

Andrey


A number like 247 8000 could be:

  • the local number 247 8000 in an unspecified area in an unspecified country, maybe +1 212 247 8000
  • the local number 8000 in the 247 area code in an unspecified country, maybe +269 247 8000
  • the local number 8000 in the 247 country code, as +247 8000

Without further context it is impossible to tell.

The + symbol is vital in identifying the country code part.

like image 24
dave singer Avatar answered Sep 26 '22 22:09

dave singer