使用 PHP 快速检查输入文本中的电话号码格式?

发布于 2024-09-11 05:52:19 字数 35 浏览 6 评论 0 原文

从表单输入文本中快速检查电话号码格式的最有效方法是什么?

What's the most efficient way to quickly check for telephone number format from a form input text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逐鹿 2024-09-18 05:52:19

最简单的方法是去掉所有不是数字的东西,然后计算有多少位数字。这样您就不必向用户强制采用某种格式。至于您应该检查多少号码...取决于您是否只检查本地号码、美国号码、国际号码等...但是例如:

$number="(123) 123-1234";
if (strlen(preg_replace('~[^0-9]~','',$number)) == 10) {
  // 10 digit number (US area code plus phone number)
}

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:

$number="(123) 123-1234";
if (strlen(preg_replace('~[^0-9]~','',$number)) == 10) {
  // 10 digit number (US area code plus phone number)
}
冰之心 2024-09-18 05:52:19

这确实取决于位置。对于区号的长度和整个号码的长度,各地有不同的约定。更加宽容一点会更好。

确保它是可选的“+”的正则表达式,后跟任意数量的数字将是最低要求。允许可选的破折号(“-”)或空格分隔数字组可能是一个很好的补充。另外,分机号码怎么样?

事实上,最好只是检查它是否包含一些数字。

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.

流云如水 2024-09-18 05:52:19

如果您只处理美国电话号码,则可以遵循使用三个文本框的常见 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.

jJeQQOZ5 2024-09-18 05:52:19

如果您只处理美国电话号码,您可以遵循使用三个文本框的常见 UI 设计 = 一个用于区号 + 第二个用于交换 + 第三个用于号码。这样,您就可以测试每个数字是否仅包含数字,以及输入的数字位数是否正确。

如果您要处理世界各地的号码,这种情况就会出现问题,因为有些国家根本不使用区号,而且各个国家/地区的号码长度各不相同。

用户号码可以短至 3 位数字,也可以长至 11 或 12 位数字。使用的区号范围可以是 1 到 6 位数字。数据还需要与国家/地区代码一起存储,以便有机会正确格式化其显示。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文