报名表(验证邮件)

时间:2013-10-29 15:37:59

标签: php mysql registration

我有一个项目,我需要创建注册表单并将其插入数据库。

我每次尝试注册时都会被卡住,它会弹出一条消息“请输入有效的电子邮件格式”。关于电子邮件的正则表达式,该怎么办?

以下是代码

<?php
    require_once('sqlconnect.inc.php');

    session_start();
    if (isset($_POST["Register"]))
        {
            $email = $_POST['email'];
            $memberName = $_POST['membername'];
            $passw = $_POST['password'];
            $conPassw = $_POST['conpassword'];

            if($email=='')
            {
            echo "<p>Please enter the Email address</p>";
            echo "<a href=\"signup.php\">Back to Registration Page!</a>"; 
            exit();
            }

            if($memberName=='')
            {
            echo "<p>Please enter the Member Name</p>";
            echo "<a href=\"signup.php\">Back to Registration Page!</a>"; 
            exit();
            }

            if($passw=='')
            {
            echo "<p>Please enter Password</p>";
            echo "<a href=\"signup.php\">Back to Registration Page!</a>"; 
            exit();
            }

            if($conPassw=='')
            {
            echo "<p>Please enter Confirm Password</p>";
            echo "<a href=\"signup.php\">Back to Registration Page!</a>"; 
            exit();
            }
            else {

            $email = $_POST['email'];
            $pattern = '/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/';
            if (preg_match($pattern, $email))
            {
                echo "<p>Please enter a valid email format<p>";
                }
                else{
                $memberName = strtolower(trim($_POST['membername']));
                $_SESSION['name'] = $_POST['membername'];
                $pattern1 = '/^[a-z][a-z ]*$/i';
                if (preg_match($pattern1, $memberName))
                    {
                    echo "<p>Member name must contain only letters, space and hypen</p>";
                    }
                    else{

                    $pattern2 = '^[a-zA-Z0-9]+$';
                    if (preg_match($pattern2, $passw))
                        {
                        echo "<p>Password must only contain numbers and letters!</p>";
                        }
                        else{

                                if($passw<>$conPassw)
                                {
                                    echo "<p>Passwords does not match!</p>";
                                    $passw="";
                                    $conPassw="";
                                }
                                else{

                                $conn = @mysqli_connect($host, $user, $pswd, $dbnm);
                                if (!$conn)
                                die ("<p>Couldn't connect to the server!<p>");
                                $SelectDatabase = @mysqli_select_db($conn,"s7259476_db")
                                or die("<p>The database is not available.</p>");

                                    $insertDatabase = "INSERT INTO team VALUES('NULL','$email','$password','$memberName',CURDATE(),0)";
                                    $queryResult = @mysqli_query($conn, $insertDatabase)
                                    or die ("<p>Email already exists.Please enter another email id</p>");
                                    echo"<p>Data entered into friends table successfully</p>";
                                    echo "<p>Welcome"."   ".$_SESSION['name']."</p>";
                                }
                            }
                        }
                    }
                }
            }


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/chtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="description" content="Web Programming :: Assignment 2" />
    <meta name="Keywords" content="Web, programming" />
    <title>Register Page</title>
</head>

<body>

<form id='register' action='signup.php' method='POST'>
        <fieldset >
            <legend><h1>My Team System Registration Page</h1></legend>
            <?php $email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_STRING) : ''; ?>
            <?php $memberName = isset($_POST['membername']) ? filter_var($_POST['membername'], FILTER_SANITIZE_STRING) : ''; ?> 
            <div class="elements">
            <label for='email' >Email:</label>
                <input type='text' name='email' id='email' maxlength="50"  value="<?php echo $email; ?>" />
            </div>
                    <br />
            <div class="elements">
            <label for='membername' >Member Name:</label>
                <input type='text' name='membername' id='membername' maxlength="50" value="<?php echo $memberName; ?>" />
            </div>
                    <br />
            <div class="elements">
            <label for='password' >Password:</label>
                <input type='password' name='password' id='password' maxlength="50" />
            </div>
                    <br />
            <div class="elements">
            <label for='conpassword' >Confirm Password:</label>
                <input type='password' name='conpassword' id='conpassword' maxlength="50" />
            </div>
                    <br />
            <div class="submit">
                <input type='submit' name='Register' value='Register' />
                <input type='reset' name='Submit' value='Clear' />
                    <br />
            <div class="elements">
            <a href="Index.php">Home</a> 
        </fieldset>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

请勿使用正则表达式验证电子邮件地址。 PHP有filter_var,快速且易于使用。使用正则表达式正确验证电子邮件地址是非常痛苦的(有一个正则表达式可以在某处正确验证它们,并且它非常大!)。

使用filter_var验证电子邮件地址的一些典型代码可能如下所示:

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    echo "Invalid email address";
}