2012-10-13 192 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; 
}