在PHP中验证英国电话号码 [英] Validating UK phone numbers in PHP
问题描述
现在它验证没有($ phone))
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b if(!$ phone || strlen($ phone)< 8)
{
$ error。=请输入您的电话号码,不得有空格。< br />
{
$ error。=请在电话号码栏中输入数字字符< br />;
正则表达式模式,这是你将需要的。
这种模式,例如:
$ pattern =/ ^(\ + 44 \s?7 \ {3} | \(?07 \ {3} \)?)\ s?\d {3} \s \d {3} $ /?;
$ match = preg_match($ pattern,$ input);
$ b $ if($ match!= false){
//我们有一个有效的电话号码
} else {
//我们有一个无效的电话号码
$ b $ p
$ b $ p $这个模式可以匹配+44包含或不包含例如$ b $
$ $ $ $ $
$ b $(07222) 555555
+44 7222 555 555
这些不会
7222 555555
+44 07222 555555
(+447222)555555
正则表达式中提供了一些教程/备忘单等等的站点。 / p>
http://regexlib.com/Default.aspx 以及一个很好的堆栈溢出帖子: 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:
Phone numbers are typically horrible for regex patterns, which is what you will need. This pattern for example: That pattern will match with +44 included or not e.g. all these will match: These won't 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 这篇关于在PHP中验证英国电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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 />";
}
$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
}
07222 555555
(07222) 555555
+44 7222 555 555
7222 555555
+44 07222 555555
(+447222) 555555