开发者

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?


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:

  • 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜