美国电话号码验证 [英] US phone number validation

查看:213
本文介绍了美国电话号码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在努力验证在

表单上输入的电话号码,但遇到了一个我不明白如何修复的问题。我可以

处理大多数情况,当它是常规美国格式(555-555-5555或

(555)555-5555或555 555 5555)但我当条目是

连续十个数字时没有别的东西(5555555555)。它不仅没有验证,而且当我将变量调用到
时它显示为2147483647,检查它是如何设置的。我知道我可以将表格分成三个

值,如< input name =" phone [area]"> < input name =" phone [code]">和

< input name =" phone [num]">消除这种情况,但我不喜欢这种外观,我知道有一种更好的方法,我找不到。


我尝试使用谷歌搜索有关preg_replace语句的信息,这可能是我需要的b


$ b $ 4和第4和第7位数字,如果还没有那个)但

我没有成功。


我需要做什么来处理这个?

I''m still working on validating the phone numbers that are entered on a
form but have come across a problem I don''t understand how to fix. I can
handle most instances when it''s in regular US formats (555-555-5555 or
(555) 555-5555 or 555 555 5555) but I''m having trouble when the entry is
ten consecutive numbers with nothing else (5555555555). Not only does it
not validate, but it shows as 2147483647 when I call the variable to
check to see how it is set. I know I could break the form up into three
values as in <input name="phone[area]"> <input name="phone[code]"> and
<input name="phone[num]"> to eliminate that happening but I don''t like
the look of that and I know there''s a better way that I haven''t found.

I tried googling for information on a preg_replace statement that could
do what I need (check the phone number and add a space between the 3rd
and 4th and the 6th and 7th digits if there isn''t one there already) but
I haven''t been successful.

What do I need to do to handle this?

推荐答案

>我还在努力验证在
>I''m still working on validating the phone numbers that are entered on a
表单中输入的电话号码但是遇到了一个我不明白如何解决的问题。我可以处理大多数情况,当它采用美国常规格式(555-555-5555或
(555)555-5555或555 555 5555)但我在进入时遇到麻烦是连续十个没有别的数字(5555555555)。它不仅没有验证,而且当我调用变量来检查它是如何设置时它显示为2147483647。


电话号码不是整数。他们是字符串。特别是,

领先的零数。不要把它们当成数字。不要使用数字格式打印

。一个10位数的电话号码可能不适合

32位整数。虽然你不会发现美国的电话号码开始时为零,但是在美国的国际电话中,无论你是否拨打前导零,确实无关紧要。另外,

817-555-0001和817-555-1之间存在差异。


美国电话号码偶尔也有扩展,这使得

他们超过10位数。

我知道我可以将表格分成三个
值,如< input name =" phone [area] "> < input name =" phone [code]">和
< input name =" phone [num]">消除这种情况,但我不喜欢它的外观,我知道有一种更好的方法,我找不到。


我的首选是将它存储为数字字符串(varchar)和

以我喜欢的标准格式显示它。在输入时,最好首先删除所有非数字,然后验证它。

我试图在google搜索有关preg_replace语句的信息
做我需要的(检查电话号码,并在第3和第4和第6和第7位之间添加一个空格,如果还没有那个)但是
我没有'没有成功。
form but have come across a problem I don''t understand how to fix. I can
handle most instances when it''s in regular US formats (555-555-5555 or
(555) 555-5555 or 555 555 5555) but I''m having trouble when the entry is
ten consecutive numbers with nothing else (5555555555). Not only does it
not validate, but it shows as 2147483647 when I call the variable to
check to see how it is set.
Telephone numbers are not integers. They are strings. In particular,
leading zeroes count. Don''t treat them as numbers. Don''t print
them using numeric formats. A 10-digit phone number may not fit in
a 32-bit integer. Although you won''t find US phone numbers beginning
with zero, on international calls from the US it does matter whether
you dial a leading zero or not. Also, there''s a difference between
817-555-0001 and 817-555-1.

US telephone numbers also occasionally have extensions, which makes
them longer than 10 digits.
I know I could break the form up into three
values as in <input name="phone[area]"> <input name="phone[code]"> and
<input name="phone[num]"> to eliminate that happening but I don''t like
the look of that and I know there''s a better way that I haven''t found.
My preference would be to STORE it as a digit string (varchar) and
display it in some standardized format I like. On input, it''s probably
best to first strip out all the non-digits and then validate it.
I tried googling for information on a preg_replace statement that could
do what I need (check the phone number and add a space between the 3rd
and 4th and the 6th and 7th digits if there isn''t one there already) but
I haven''t been successful.




Gordon L. Burditt



Gordon L. Burditt


^(\ D *)?(\\ \\ n {3})(\ D *)?(\d {3})(\ D *)?(\d {4})
^(\D*)?(\d{3})(\D*)?(\d{3})(\D*)?(\d{4})





如果我的正则表达式内存正确服务:

这将匹配xYYYxYYYXYYYY,其中每个x都是非数字,并且

可选,并且Y是一个数字。第2组,第4组和第6组分别是区号,

前缀和后缀。


1234567890 - 匹配

(123)456-7890 - 匹配

A123BCD4567890 - 匹配

123-456-7890 - 匹配


另一种选择是剥去所有非数字,看看它是否有10个字符

长:

正则表达式:\ D,全局修饰符打开(我认为是preg_replace_all)


将电话号码存储为字符串,所有非数字都被删除,然后

显示你喜欢的方式。



if my regex memory serves correctly:
That will match xYYYxYYYxYYYY, where each x is a non-digit and is
optional, and Y is a digit. Group 2, 4, and 6 are the area code,
prefix, and suffix, respectively.

1234567890 - match
(123) 456-7890 - match
A123BCD4567890 - match
123-456-7890 - match

Another option is to strip all non-digits and see if its 10 characters
long:
regex: \D, with the global modifier on (preg_replace_all i think)

Store phone numbers as strings with all non-digits stripped out, then
display them how you like.

这篇关于美国电话号码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆