views:

64

answers:

2

Hi

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?

A: 

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.

Andrey
+1  A: 

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.

Andy
it doesn't work. I entered my mobile number without plus and set US as default. It was not able to parse it. So it is useless. In my answer i gave a case when NO certain assumption may be made.
Andrey
Without knowing your phone number I can't speak to its effectiveness in your situation, but when I enter mine or any other 10-digit numbers without a leading plus and set US, it parses correctly. I'm not sure what the difference would be.
Andy