在 PHP 中验证英国电话号码

发布于 2024-12-15 05:05:32 字数 372 浏览 3 评论 0 原文

我购买了联系表。很棒的小事情,但我需要转换电话号码的验证以允许英国号码格式 - 换句话说,允许空格。

现在它的验证不带空格,最小长度为 8 个字符:

if(is_numeric($phone))
{
    if(!$phone || strlen($phone) < 8)
    {
        $error .= "Please enter your phone number without spaces.<br />";
    }
}
else
{
    $error .= "Please enter numeric characters in the phone number field.<br />";
}

I purchased a contact form. Great little thing but I need to convert the validation for the phone number to allow for UK number formats - in other words, to allow for spaces.

Right now it validates without spaces and has a minimum length of 8 characters:

if(is_numeric($phone))
{
    if(!$phone || strlen($phone) < 8)
    {
        $error .= "Please enter your phone number without spaces.<br />";
    }
}
else
{
    $error .= "Please enter numeric characters in the phone number field.<br />";
}

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

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

发布评论

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

评论(6

如果没结果 2024-12-22 05:05:32

电话号码对于正则表达式模式来说通常很糟糕,而这正是您所需要的。

例如这个模式:

$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";

$match = preg_match($pattern,$input);

if ($match != false) {
    // We have a valid phone number
} else {
    // We have an invalid phone number
}

该模式将与包含或不包含的+44匹配,例如

所有这些都将匹配:

07222 555555

(07222) 555555

+44 7222 555 555

这些不会

7222 555555

+44 07222 555555

(+447222) 555555

有很多网站提供正则表达式的教程/备忘单等,请尝试其中一些:

http://regexlib.com/Default.aspx

以及一篇非常好的堆栈溢出帖子:

用于电话号码验证的综合正则表达式

Phone numbers are typically horrible for regex patterns, which is what you will need.

This pattern for example:

$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";

$match = preg_match($pattern,$input);

if ($match != false) {
    // We have a valid phone number
} else {
    // We have an invalid phone number
}

That pattern will match with +44 included or not e.g.

all these will match:

07222 555555

(07222) 555555

+44 7222 555 555

These won't

7222 555555

+44 07222 555555

(+447222) 555555

There are a load of sites that offer tutorials / cheat sheets etc. for regular expressions try some of these:

http://regexlib.com/Default.aspx

as well as a very good stack overflow post:

A comprehensive regex for phone number validation

任性一次 2024-12-22 05:05:32

所以你只想留出空间?

然后你可以使用 str_replace() 忽略开头的空格:

$phone = str_replace(' ', '', $phone);

So you just want to allow spaces?

Then you could use str_replace() to ignore spaces, right at the beginning:

$phone = str_replace(' ', '', $phone);
一个人的旅程 2024-12-22 05:05:32

您可以将输入解析为整数,然后验证位数是否正确;

$mobileNumber = intval($mobileNumber);

if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//number has the right number of digits for a UK mobile number
} else {
//number does not have the right number of digits
}

解释;

$mobileNumber = intval($mobileNumber);

这会从字符串中删除所有非数字字符以及前导零(例如空格、括号、加号、小数等);

*if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {*

然后,此正则表达式检查字符串是否包含 12 或10 位数字,涵盖 447712345678(12 位数字)或 7791234567(10 位数字)。它在两个子子句 (^\d{12}$) 或 (^\d{10}$) 中进行匹配,克拉 (^) 和美元 ($) 表示匹配从开头 (^) 到结尾的所有内容。字符串的最末尾 ($)。 \d 代表任意数字,后面的 {12} 表示与前面的语句匹配该次数。

这并不意味着该号码有效,您可以使用 twillio 等 API 对号码进行额外验证 https://www.twilio.com/help/faq/sms/does-twilio-check-to-see-if-phone-numbers-can-receive-sms

例如;

$mobileNumber = '+447791234567';

$mobileNumber = intval($mobileNumber); //$mobileNumber is now 447791234567

    if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//$mobileNumber matches (^\d{12}$) and is valid
        }

您还可以添加一个检查,以确保号码以“07”或“447”开头,这是英国手机所必需的,如下所示;

function isValidMobile($aNumber){
    $aNumber = intval($aNumber);
    return preg_match('/(^\d{12}$)|(^\d{10}$)/', $aNumber) && preg_match('/(^7)|(^447)/', $aNumber);
}

intval 删除前导零,因此正则表达式检查前导 7 或 447。

You could parse the input to an integer, then validate for the correct number of digits;

$mobileNumber = intval($mobileNumber);

if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//number has the right number of digits for a UK mobile number
} else {
//number does not have the right number of digits
}

Explained;

$mobileNumber = intval($mobileNumber);

This removes all non numerical characters from the string, and leading zero (eg spaces, brackets, plus signs, decimals etc);

*if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {*

This regular expression then checks that the string contains either 12 or 10 digits which would cover 447712345678 (12 digits) or 7791234567 (10 digits). It is matched in two sub clauses (^\d{12}$) or (^\d{10}$) the carat (^) and dollar ($) represent to match everything from the very beginning (^) and to the very end ($) of the string. the \d represents any digit, and the following {12} means match the previous statement that number of times.

This does not mean that the number is valid, you could use an API like twillio to do additional validation of the numebr https://www.twilio.com/help/faq/sms/does-twilio-check-to-see-if-phone-numbers-can-receive-sms

For example;

$mobileNumber = '+447791234567';

$mobileNumber = intval($mobileNumber); //$mobileNumber is now 447791234567

    if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//$mobileNumber matches (^\d{12}$) and is valid
        }

You could also add a check to ensure that number starts with either '07' or '447', required for UK mobiles, like this;

function isValidMobile($aNumber){
    $aNumber = intval($aNumber);
    return preg_match('/(^\d{12}$)|(^\d{10}$)/', $aNumber) && preg_match('/(^7)|(^447)/', $aNumber);
}

the intval removes leading zero,so the regular expression checks for a leadin 7 or 447.

小镇女孩 2024-12-22 05:05:32

您正在使用的 is_numeric 函数甚至不是美国电话号码的合适选择。例如,它接受像 0xABCDEF 这样的十六进制数字,它应该拒绝。

对于像这样的简单文本匹配,正则表达式通常是最简单的解决方案。正则表达式指定文本的模式。 PHP 具有可让您搜索或替换文本中的正则表达式匹配项的函数。

如果将电话​​号码定义为至少 7 个字符且仅包含数字和空格的字符串,则相应的正则表达式将为 /^[0-9 ]{7,}$/。括号内的文本表示一组字符,{7,} 表示我们正在连续查找其中至少 7 个字符,^$ 表示我们的匹配应该从字符串的开头开始并在字符串的末尾结束。 PHP 文档有一节更详细地解释了正则表达式

您可以使用 preg_match 函数
确保电话号码匹配:

if (preg_match('/^[0-9 ]{7,}$/', $phone)) {

The is_numeric function that you're using isn't really even a suitable choice for American phone numbers. For example it accepts hexadecimal numbers like 0xABCDEF, which it should reject.

For simple text matching like this, regular expressions are often the easiest solution. A regular expression specifies a pattern of text. PHP has functions to let you search for or replace regular expression matches in text.

If you define a phone number as a string of at least 7 characters containing only digits and spaces, the corresponding regular expression would be /^[0-9 ]{7,}$/. The text inside the brackets represents a set of characters, the {7,} indicates that we're looking for at least 7 of these characters in a row, and the ^ and $ indicate that our match should start at the beginning of the string and end at the end of the string. The PHP documentation has a section explaining regular expressions in greater detail.

You would use the preg_match function to
ensure the phone number matched:

if (preg_match('/^[0-9 ]{7,}$/', $phone)) {
尝蛊 2024-12-22 05:05:32

这与具有多种标点符号的所有英国格式相匹配:

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{5}\)?[\s-]?\d{4,5}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$

使用此模式提取各个部分

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d-]+)(?:((?:x|ext\.?\s?|\#)\d+)?)$

国家/地区代码以 $1 为单位(对于国家/地区格式为空)。 NSN 的价格为 2 美元。可选扩展的价格为 3 美元。

删除 $2 中的所有非数字以进行进一步处理。下一步是确保 NSN 位于有效范围内且长度有效(9 或 10 位数字,具体取决于范围)。

模式列表太长,无法在此处重现,但可以在以下位置找到:

http://aa- asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

This matches all UK formats with a wide variety of punctuation:

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{5}\)?[\s-]?\d{4,5}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$

Extract the various parts using this pattern

  ^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d-]+)(?:((?:x|ext\.?\s?|\#)\d+)?)$

The country code is in $1 (and is null for national format). The NSN is in $2. The optional extension is in $3.

Remove all non-digits from $2 for further processing. The next step is to make sure the NSN is in a valid range and is a valid length (either 9 or 10 digits, depending on the range).

The list of patterns is too long to reproduce here but is available at:

http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers

妖妓 2024-12-22 05:05:32

我根据[此处][1]提到的标准找到了一个使用javascript的解决方案。**它不是正则表达式。**如果您使用此解决方案,则需要注意三件事

  • 验证可以在英国
  • 空间内使用不能额外。您需要输入不带空格的号码
  • 号码必须以零开头
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script >
var name=window.prompt("Enter cell number");
var sixformat=["013397","013398","013873","015242","015394","015395","015396","016973","016974","017683","017684","017687","019467","019755","019756"];
var twoformat=["01","02","03","05","07","08","09"];
sixdi=name.substring(0,6);
twodi=name.substring(0,2);
document.write("phone number:",name,"<br>");
document.write("phone-number length:",name.length);
if(sixformat.includes(sixdi)&&name.length==11)
{
    document.write("<br><h1>valid</h1>");
}
else if(sixdi=="016977"&&name.length==10)
{
    document.write("<br><h1>valid<h1>");
}
else if(twoformat.includes(twodi)&&(name.length==11))//011########
{
    document.write("<br><h1>valid</h1>");
}
else if(twodi=="01"&&(name.length==10))
{
    document.write("<br><h1>valid</h1>");
}
else if(name.substring(0,4)=="0800"&&(name.length==10))
{
    document.write("<br><h1>Valid</h1></br>");
}
else
{
    document.write("<h1>invalid</h1>");
}
</script>
</body>
</html>

I have found a solution with javascript according to the standards mentioned [here][1].**its not regex.**if you are using this solution there are three things to take care

  • validation can be used inside the UK
  • space cant be added. you need to type in your number without spaces
  • number has to begin with zero

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script >
var name=window.prompt("Enter cell number");
var sixformat=["013397","013398","013873","015242","015394","015395","015396","016973","016974","017683","017684","017687","019467","019755","019756"];
var twoformat=["01","02","03","05","07","08","09"];
sixdi=name.substring(0,6);
twodi=name.substring(0,2);
document.write("phone number:",name,"<br>");
document.write("phone-number length:",name.length);
if(sixformat.includes(sixdi)&&name.length==11)
{
    document.write("<br><h1>valid</h1>");
}
else if(sixdi=="016977"&&name.length==10)
{
    document.write("<br><h1>valid<h1>");
}
else if(twoformat.includes(twodi)&&(name.length==11))//011########
{
    document.write("<br><h1>valid</h1>");
}
else if(twodi=="01"&&(name.length==10))
{
    document.write("<br><h1>valid</h1>");
}
else if(name.substring(0,4)=="0800"&&(name.length==10))
{
    document.write("<br><h1>Valid</h1></br>");
}
else
{
    document.write("<h1>invalid</h1>");
}
</script>
</body>
</html>

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