2014-02-07 49 views
-1

创建一个表单,这是我的联系形式的一部分试图在PHP,但错误

我想让它,以便它仅验证当用户输入一个电话号码

这里就是我试图

$telephone = $_POST['Telephone_Number']; 

$telephone_exp = '/^[+]?([\d]{0,3})?[\.\-\s]?([\d]3)[\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/'; 
if ($telephone > 0) && (preg_match($telephone_exp, $telephone)) 
{ 
    $error_message .= 'The Telephone Number you entered does not appear to be valid.<br />'; 
} 
+0

没关系,你可以给我一些暗示?这将是真正伟大 – user202476

回答

0

验证前添加if(!empty($telephone))。这样,只有在提供电话价值时才能执行验证。

2

尝试做它喜欢:

$telephone = $_POST['Telephone_Number']; 

$telephone_exp = '/^[+]?([\d]{0,3})?[\.\-\s]?([\d]3)[\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/'; 
if (isset($_POST['Telephone_Number']) && preg_match($telephone_exp, $telephone)) 
{ 
    $error_message .= 'The Telephone Number you entered does not appear to be valid.<br />'; 
} 
1

这是正确的语法

if ($telephone > 0 && preg_match($telephone_exp, $telephone)){ 
} 

你有

if ($telephone > 0) && (preg_match($telephone_exp, $telephone)) 
+0

好让我尝试和感谢 – user202476

0

造成的误差只有打字,用括号。

正确:if (($telephone > 0) && (preg_match($telephone_exp, $telephone)))

错误:if ($telephone > 0) && (preg_match($telephone_exp, $telephone))