2

好的。所以这是我下面的代码。我正在尝试遵循有关验证电子邮件的 webtuts 教程。但我的样本没有成功。它应该提醒用户它输入了无效的电子邮件。所以我的伙伴所做的是他创建了“show_warning”jquery 函数来允许我显示我的 $msg。但它不起作用。我的逻辑错了吗?

<?php

if(isset($_POST['username']) && !empty($_POST['username']) AND
   isset($_POST['password']) && !empty($_POST['password']) AND
   isset($_POST['email']) && !empty($_POST['email']) AND
   isset($_POST['role_id']) && !empty($_POST['role_id'])) 
    {
        $username = ($_POST['username']);
        $password = ($_POST['password']);
        $email = ($_POST['email']);
        $role_id = ($_POST['role_id']);  

            if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
            {  
                $msg = 'The email you entered is invalid. Please try again.';
            }
                else
            {  
                $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';  
            }     
    } 
?>

=======================================

<div id="main-content">
  <div id="create-user">

      <h1>Create User</h1>
      <form action="" method="post">
      <table id="userform" width="600px" border=0>
        <tr>
          <td><label for="username">Username</label>
            <input type="text" id="username" name="username" /></td>
          <td><label for="password">Password</label>
            <input type="text" id="password" name="password" /></td>
        </tr>
        <tr>
          <td><label for="email">Email</label>
            <input type="text" id="email" name="email" /></td>
          <td><label for="role_id">Role</label>
            <select>
              <?php $roles = load_roles() ;?>
              <?php foreach ($roles as $role): ?>
              <option value='<?php echo $role['role_id']; ?>'><?php echo $role['role']; ?></option>
              <?php endforeach; ?>
            </select></td>
        </tr>
        <tr>
          <td><input type="submit" id="submit" name="save_user" value="Create User"></td>
        </tr>
      </table>
    </form>
  </div>
</div>
4

3 回答 3

6

用于验证电子邮件 php 提供

$email="test@gmail.com" //your email to validate here

if(filter_var($email, FILTER_VALIDATE_EMAIL)){

       echo "E-mail is valid";
}
else
{
      echo "E-mail is not valid";
}

你不能使用eregi. 您可以使用preg_match()

有关更多验证功能,请点击此链接

http://php.net/manual/en/filter.filters.validate.php

于 2013-05-16T04:50:09.837 回答
4

eregi()自 PHP 5.3.0 起,该函数已被弃用。强烈建议不要依赖此功能。

所以这将是实际原因

AND preg_match()与 i (PCRE_CASELESS) 修饰符是建议的替代方法。

作为对 Yadav Chetan 的回答,使用FILTER_VALIDATE_EMAIL那些 regx

于 2013-05-16T04:44:54.450 回答
0
 if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
            {  
                echo $msg = 'The email you entered is invalid. Please try again.';
            }
                else
            {  
                echo $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';  
            }   
于 2013-05-16T04:57:46.487 回答