2012-10-13 205 views
3

我试图验证我的用户名为电子邮件地址,但PHP不让我这样做!这里有什么问题?php中的电子邮件验证

//This checks if all the fields are filled or not 
if (!empty($_POST['username']) || 
!empty($_POST['password']) || 
!empty($_POST['repassword']) || 
!empty($_POST['user_firstname']) || 
!empty($_POST['user_lastname'])){ 
header('Location: register.php?msg=You didn\'t complete all of the required fields'); 
} 

if (filter_var($_POST['username'], FILTER_VALIDATE_EMAIL) === false){ 
$errors[] = 'The email address you entered is not valid'; 
} 

这是我在register.php

<form action="createuser.php" method="post" name="registration_form" id="registration_form"> 
<label>Email</label> 
<input name="username" type="text" id="username" size="50" maxlength="50" /><br /> 
+0

发送给用户一些带有首次密码的电子邮件而不是验证超过这个数量就足够了吗? – budwiser

+0

我只是想确保用户输入一个电子邮件作为用户名! – thedullmistro

+0

你尝试过的电子邮件是什么,或者没有电子邮件? FILTER_VALIDATE_EMAIL允许本地邮件地址不指定全局域,例如“user @ localhost”是一个有效的电子邮件。 – Sven

回答

5

错字?

header('Location: register.php?msg=You didn't complete all of the required fields'); 
              ^---unescaped embedded quote 

您的空逻辑也是错误的。您正在检查是否有任何字段为空(不是)(然后填写),然后抱怨他们没有填写。删除!以反转逻辑。

if (empty(...) || empty(...) || etc...) 
+0

谢谢!这仍然不能解决电子邮件问题虽然:) – thedullmistro

+1

如何PHP不让你这样做吗?它拒绝所有的电子邮件地址? –

+0

@Marc B这是一种说法,不验证:D – budwiser

0

用于起动形式,看语法高亮为什么你要分析错误。

header('Location: register.php?msg=You didn't complete all of the required fields'); 

需要成为:

header('Location: register.php?msg=You didn\'t complete all of the required fields'); 
+0

哦哦,这就是它现在的样子! 仍然不能解决我的问题。 – thedullmistro

1

你不会去验证电子邮件,因为你的if语句是错误的..它检查是否有任何帖子不是空的。

if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname'])) { 
0

更换它你怎么样用javascript window.location的?有时候头文件的功能很敏感。并且还在你的表单中放了一个提交按钮,因为默认字段为空时为

if(isset($_POST['your_submit_button_name'])){ 

if (empty($_POST['username']) || 
empty($_POST['password']) || 
empty($_POST['repassword']) || 
empty($_POST['user_firstname']) || 
empty($_POST['user_lastname'])){ 
?> 
<script> 
window.location = 'register.php?msg=You didn\'t complete all of the required fields'; 
</script> 
<?php 
} 

    if (filter_var($_POST['username'], FILTER_VALIDATE_EMAIL) === false){ 
    $errors[] = 'The email address you entered is not valid'; 
    } 
} 

注意:我删除“!”因为你陷阱空的领域之前你的空功能。

0

尝试使用此解决方案:

$FormData = $_POST; 
if(isset($FormData['button_name'])){ 
    $Errors = array(); 
    foreach ($$FormData as $key => $value) { 
     if(empty($value)) $Errors[] = 'Some message'; 
     if($key = 'username'){ 
      if(filter_var($value, FILTER_VALIDATE_EMAIL){ 
       $Errors[] = 'The email address you entered is not valid'; 
      } 
     } 
    } 
    if(empty($Errors)){ 
     // @todo Do some action 
    } else { 
     header('Location: register.php?msg=You didn\'t complete all of the required fields'); 
    } 
} 
2

,而不是这种使用正则表达式来验证您的电子邮件地址

function check_email_address($email) { 
    // First, we check that there's one @ symbol, 
    // and that the lengths are right. 
    if (!preg_match("^[^@]{1,64}@[^@]{1,255}$", $email)) { 
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols. 
    return false; 
    } 
    // Split it into sections to make life easier 
    $email_array = explode("@", $email); 
    $local_array = explode(".", $email_array[0]); 
    for ($i = 0; $i < sizeof($local_array); $i++) { 
    if 
(!preg_match("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%& 
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", 
    $local_array[$i])) { 
     return false; 
    } 
    } 
    // Check if domain is IP. If not, 
    // it should be valid domain name 
    if (!preg_match("^\[?[0-9\.]+\]?$", $email_array[1])) { 
    $domain_array = explode(".", $email_array[1]); 
    if (sizeof($domain_array) < 2) { 
     return false; // Not enough parts to domain 
    } 
    for ($i = 0; $i < sizeof($domain_array); $i++) { 
     if 
(!preg_match("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])| 
↪([A-Za-z0-9]+))$", 
$domain_array[$i])) { 
     return false; 
     } 
    } 
    } 
    return true; 
} 

,然后检查其是否返回true重定向它的位置,如果不是那么简单抛出一个错误

+0

+1用于正则表达式 –

0
function check_email($check) { 
$expression = "/^[a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/"; 
if (preg_match($expression, $check)) { 
    return true; 
} else { 
    return false; 
} 
} 

现在使用这种方法:

if(!check_email($_REQUEST['ContactEmail'])){ 
    $register_error .="Enter the correct email address!<br />"; 
    $reg_error=1; 
}