3

我正在尝试将我的用户名验证为电子邮件地址,但是 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 />
4

7 回答 7

5

错字?

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

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

if (empty(...) || empty(...) || etc...)
于 2012-10-13T23:34:05.627 回答
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 将其重定向到位置,如果不是,则简单地抛出一个错误

于 2012-10-16T06:47:54.327 回答
1

您将无法验证电子邮件,因为您的 if 语句是错误的 .. 它正在检查是否有任何帖子不为空。

将其替换为

if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['repassword']) || empty($_POST['user_firstname']) || empty($_POST['user_lastname'])) {
于 2012-10-13T23:40:03.180 回答
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');
于 2012-10-13T23:33:51.830 回答
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';
   }
}

注意:我删除“!” 在你的空函数之前,因为你捕获了空的字段。

于 2012-10-14T01:45:43.067 回答
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');
    }
}
于 2012-10-16T06:28:16.340 回答
0
function check_email($check) {
$expression = "/^[a-zA-Z0-9._-]+@[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; 
}
于 2013-05-14T12:51:20.677 回答