希望验证带有区号的美国电话号码
我正在开发一个功能来验证用户提交的美国电话号码,该号码可以以人们通常使用的任何流行号码格式提交。到目前为止我的代码如下:
$number = '123-456-7890';
function validate_telephone_number($number) {
$formats = array(
'###-###-####',
'(###)###-###',
'(###) ###-###',
'##########'
);
$number = trim(preg_replace('[0-9]', '#', $number));
if (in_array($number, $formats)) {
return true;
} else {
return false;
}
}
首先,这段代码似乎不起作用,并且对所有提交的数字返回 false。我似乎找不到我的错误。
其次,我正在寻找一种简单的方法来仅允许来自一系列特定允许区号的电话号码。有什么想法吗?
I'm working on a function to validate a US phone number submitted by a user, which can be submitted in any of the popular number formats people usually use. My code so far is as follows:
$number = '123-456-7890';
function validate_telephone_number($number) {
$formats = array(
'###-###-####',
'(###)###-###',
'(###) ###-###',
'##########'
);
$number = trim(preg_replace('[0-9]', '#', $number));
if (in_array($number, $formats)) {
return true;
} else {
return false;
}
}
First off, this code does not seem to be working, and returns false on all submitted numbers. I can't seem to find my error.
Secondly, I'm looking for an easy way to only allow phone numbers from an array of specific allowed area codes. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您的第一个问题:
或
'/\d/'
对于第二个问题,这可能会对您有所帮助:
这将通过丢弃所有其他字符来为您提供数字中的前 3 位数字。
我不熟悉美国区号格式,因此我无法为您提供更多帮助。
额外奖励:
相当于
事实上,任何形式的语句都
可以写为
return (bool);
,但在这种情况下in_array
将始终返回布尔值,因此不需要(bool)
。For your first question:
or
'/\d/'
For the second question this may help you:
This will give you the first 3 digits in the number by discarding all other characters.
I'm not familiar with the US area codes format so I cannot help you more with this one.
Bonus:
is equivalent to
As a matter of fact any statement of the form
can be written as
return (bool) <expr>;
, but in this casein_array
will always return a Boolean so(bool)
is not needed.您的代码不会检查格式正确但无效的数字 - 例如,在美国没有以 0 或 1 开头的区号,因此可以检查这一点。此外,您的格式不允许包含国家/地区代码 - 例如,+15551234567 将被拒绝。
如果您不关心格式,只想验证输入中的数字是否等于有效的美国电话号码,您可以使用如下内容:
当然,这也将接受“foo 5555555555 bar”作为有效的number - 如果您想禁止这样做,请使 preg_replace 更具限制性(例如,仅删除括号、空格和破折号)。
Your code does not check for well formatted but invalid numbers - for example, no area code starts with 0 or 1 in the US, so this could be checked. Also, your formats do not allow for country code inclusion - +15551234567 would be rejected, for example.
If you don't care about the formatting and just want to validate if the digits in the input amount to a valid US phone number, you could use something like this:
Of course, this will also accept "foo 5555555555 bar" as a valid number - if you want to disallow that, make the preg_replace more restrictive (e.g, remove only brackets, spaces and dashes).
如果您希望在不维护大量代码的情况下执行此操作,您可以查看此 API,它验证美国号码并为号码提供多种格式 https://www.mashape.com/parsify/format
If you prefer to do this without maintaining a lot of code, you an check out this API that validates a US number and provides several formats for the number https://www.mashape.com/parsify/format
请在此处查找具有验证电话号码功能的代码项目。
Look here for a code project that has a function for validating phone numbers.