使用 PHP 快速检查输入文本中的电话号码格式?
从表单输入文本中快速检查电话号码格式的最有效方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
从表单输入文本中快速检查电话号码格式的最有效方法是什么?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
最简单的方法是去掉所有不是数字的东西,然后计算有多少位数字。这样您就不必向用户强制采用某种格式。至于您应该检查多少号码...取决于您是否只检查本地号码、美国号码、国际号码等...但是例如:
easiest way is to strip out everything that is not a number and then count how many digits there are. That way you don't have to force a certain format on the user. As far as how many numbers you should check for...depends on if you are checking for just local numbers, US numbers, international numbers, etc...but for instance:
这确实取决于位置。对于区号的长度和整个号码的长度,各地有不同的约定。更加宽容一点会更好。
确保它是可选的“+”的正则表达式,后跟任意数量的数字将是最低要求。允许可选的破折号(“-”)或空格分隔数字组可能是一个很好的补充。另外,分机号码怎么样?
事实上,最好只是检查它是否包含一些数字。
That really depends on the location. There are different conventions from place-to-place as to the length of the area code and the length of the entire number. Being more permissive would be better.
A regular expression to ensure that it is an optional '+', followed by any number of digits would be the bare minimum. Allowing optional dashes ("-") or spaces separating groups of numbers could be a nice addition. Also, what about extension numbers?
In truth, it's probably best to just check that it includes some numbers.
如果您只处理美国电话号码,则可以遵循使用三个文本框的常见 UI 设计 = 一个用于区号 + 第二个用于交换 + 第三个用于号码。这样,您可以测试每个数字是否仅包含数字,以及输入的数字位数是否正确。
您还可以使用以下常用技术:测试每个按键是否为数字(忽略非数字按键),以及在输入所需数量的字符后前进到下一个文本框进入。
第二个功能使用户可以更轻松地进行数据输入。
使用单独的文本框还可以让用户更轻松地阅读他们的输入,比阅读他们已连续正确输入十位数字更容易。
它还避免了您必须与在条目中使用不同标点符号的人打交道 - 用括号将区号括起来,在各部分之间使用连字符或点。
编辑:如果您决定只使用一个文本框,则可以在 正则表达式,这是一些很好的方法-for-phone-number-validation">这个问题。
If you are only dealing with U.S. phone numbers, you might follow the common UI design of using three textboxes = one for area code + second one for exchange + third one for number. That way, you can test that each one contains only digits, and that the correct number of digits is entered.
You can also use the common techniques of testing each keypress for numbers (ignoring keypresses that are not digits), and advancing to the next textbox after the required number of characters have been entered.
That second feature makes it easier for users to do the data entry.
Using separate textboxes also makes it a little easier for users to read their input, easier than, say, reading that they have entered ten digits in a row correctly.
It also avoids you having to deal with people who use different punctuation in their entries -- surrounding the area code with parentheses, using hyphens or dots between sections.
Edit: If you decide to stick with just one textbox, there are some excellent approaches to using regex in this SO question.
如果您要处理世界各地的号码,这种情况就会出现问题,因为有些国家根本不使用区号,而且各个国家/地区的号码长度各不相同。
用户号码可以短至 3 位数字,也可以长至 11 或 12 位数字。使用的区号范围可以是 1 到 6 位数字。数据还需要与国家/地区代码一起存储,以便有机会正确格式化其显示。
If you are dealing with numbers worldwide, this breaks down because some countries don't use area codes at all and number lengths vary from country to country.
Subscriber numbers may be as short as 3 digits or as long as 11 or 12 digits. Area codes can range from 1 to 6 digits where used. The data will also need to be stored with the country code in order to have any chance of correctly formatting it for display.