如何确认如果新用户未在电子邮件中输入“@”,则会出现错误?

时间:2011-03-29 10:34:32

标签: php

  

可能重复:
  email address validation

<?php

require_once('includes/inc_auth2.php');
require_once('includes/config.php');
//require_once('auth3.php');


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


        $title=$_POST['title'];
        $forename = $_POST['forename']; 
        $surname=$_POST ['surname'];
        $dob=$_POST ['dob'];
        $gender=$_POST ['gender'];
        $email=$_POST['email']; 
        $phone=$_POST['phone'];
        $password=$_POST ['password'];


        if (authRegister($title, $forename, $surname, $dob, $gender, $email, $phone, $password))
        {
            echo 'Thank you for registering your details, you can now login'; 

            } 
            else
                {
                    outputErrors();
                    }



        if (empty($title))
            addError("Enter your title!",LEVEL_CUSTOMER);

        if (empty($forename))
            addError("Enter your name!",LEVEL_CUSTOMER);

        if (empty($surname))
            addError("Enter your surname!",LEVEL_CUSTOMER);

        if (empty($dob))
            addError("Enter your date of birth!",LEVEL_CUSTOMER);

        if ($gender == '0')
            addError("Enter your gender!",LEVEL_CUSTOMER);

        if (empty($email))
            addError("Enter your email!",LEVEL_CUSTOMER);

        if (empty($password))
            addError("Enter your password!",LEVEL_CUSTOMER);

}

?>


<?=outputErrors()?>
 <div id="form_style">

   <form name="Create_Account" method="post" action="?page=register">

        <label> Title: </label>
            <select name="title" id="title">
             <option value="0">Select Title</option>
<?
        #
            foreach($titlesArray as $key => $value){

    // if title is selected 
            if ($title == $key)
            $selected = true; 
            else
            $selected = false; 

            echo '<option value="'.$key.'"';

            if($selected) 
            echo ' selected="selected"';

            echo '>'.$value.'</option>';
        }
    ?>
    </select> 
 <br />


    <label>Forename:</label>
        <input name="forename" type="text" value="<?php if(isset($_POST['forename'])){  echo $forename; } ?>" /> <br />

    <label>Surname:</label>
        <input name="surname" type="text" value="<?php if(isset($_POST['surname'])){  echo $surname; } ?>" /> <br />

    <label>Email:</label>
        <input name="email" type="text" value="<?php if(isset($_POST['email'])){  echo $email; } ?>" /> <br />

            <label>phone:</label>
        <input name="phone" type="text" value="<?php if(isset($_POST['email'])){  echo $email; } ?>" /> <br />

    <label>Password:</label>
        <input name="password" type="password" value="<?php if(isset($_POST['password'])){  echo $password; } ?>" /> <br />

    <label>Confirm Password:</label>
        <input name="password" type="password" value="<?php if(isset($_POST['password'])){  echo $password; } ?>" /> <br />


    <label> Gender: </label>
        <select name="gender" id="gender">
        <option value="0">Select Gender</option>
        <?
        #
        foreach($gendersArray as $key => $value){

    // if gender is selected 
            if ($gender == $key)
            $selected = true; 
            else
            $selected = false; 

            echo '<option value="'.$key.'"';

            if($selected) 
            echo ' selected="selected"';

            echo '>'.$value.'</option>';
            }
            ?>
            </select> 

           <br />


           <label>D.O.B  : </label>
            <input name="dob" type="text" value="<?php if(isset($_POST['dob'])){  echo $dob; } ?>" /> <br/>

            <input name="Submit" type="submit" value="Register" class="button">
        </form>

    </div>

3 个答案:

答案 0 :(得分:3)

要验证电子邮件地址,最好使用原生PHP filter functions,尤其是FILTER_VALIDATE_EMAIL(链接)验证过滤器。

例如:

if (empty($email) or !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    addError("Enter your email!",LEVEL_CUSTOMER);
}

答案 1 :(得分:0)

试试这个:

if(strpos($email, "@") === false) {
    addError("Email is invalid!", LEVEL_CUSTOMER);
}

对于您的其他问题,请更改确认密码框的名称,使其唯一,如下所示:

<input type="password" name="password"...
<input type="password" name="password2"...

然后在验证中:

if($password != $password2) {
    addError("Passwords do not match!", LEVEL_CUSTOMER);
}

但从安全角度来看,以纯文本格式传递密码字段并不是一个好主意。

尝试在提交表单前使用javascript验证表单,我在这里为您做了一个示例http://jsfiddle.net/dduncan/4GgTT/

答案 2 :(得分:0)

您可以对ex进行简单的验证:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$

link for Other secure ways to validate email with more examples

享受!