如何检查用户输入的电话号码是否包含国家代码?
有没有一种简单的方法来检查用户输入的电话号码是否包含国家/地区代码并验证该号码是否正确?我不使用任何特定格式,数字本身必须只是数字,没有 (、- 等。这样的验证是否可以在不询问用户国家/地区的情况下进行?诀窍是我想与所有 或者
我想这不能用正则表达式来完成(谷歌搜索了一下,发现了很多东西,但没有解决这个问题)。我正在使用Python,
也许它会产生更多。强制执行格式的意义,例如 X-YYYYYYYY...其中 X 是国家/地区代码或类似的内容?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是 Google 用于处理电话号码的库(使用 Java 语言)。然而,由于 Google 也是一家 Python 商店,我想他们可能在某个地方有可用的 Python 版本,或者你可以提取你需要的内容并翻译它。
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.
我研究了这个主题一段时间。我们有一个巨大的数据库,里面有数字,没有人知道它们是否有国家代码。一般的答案是:不可能查出来。我们有一些号码是不带国家代码 (1) 的有效美国号码,以及一些其他国家/地区的有效号码(带区号)。但对于 99% 的情况,您可以检查数字长度是否为 9,则没有国家代码。您应该强制用户输入国家/地区代码。
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.
像 247 8000 这样的号码可能是:
如果没有进一步的上下文,就无法分辨。
+ 符号对于识别国家/地区代码部分至关重要。
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.