验证,我做错了什么?

时间:2016-11-07 15:13:56

标签: php validation email

所以基本上我只想要下面这段代码:

    //basic email validation
    if(!eregi('^x[\d]{8}@student\.ncirl\.ie$', $email)){
        // Return Error - Invalid Email
        $error = true;
        $emailError = 'The email you have entered is invalid, please try again.';
    }
    if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
        $error = true;
        $emailError = "Please enter valid email address.";
    } else {
        // check email exist or not
        $query = "SELECT userEmail FROM users WHERE userEmail='$email'";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        if($count!=0){
            $error = true;
            $emailError = "Provided Email is already in use.";
        }
    }

验证电子邮件是否与电子邮件完全相似 - " x14332684@student.ncirl.ie" ,我希望它保持x在开始,在x之后有8个随机数,因为它是@之后的所有内容。只有数字是随机的,其余的应该是"必须"。我不能让它像我想要的那样验证电子邮件,使用此代码告诉我pernament"您输入的电子邮件无效,请再试一次。"。谁能为我编辑代码并指出我在做错了什么?谢谢。

2 个答案:

答案 0 :(得分:1)

这适用于preg_match。您需要做的一件事是使用分隔符包装模式,大多数使用/,如:

if(!preg_match('/^x[\d]{8}@student\.ncirl\.ie$/', $email)){
    // Return Error - Invalid Email
    $error = true;
    $emailError = 'The email you have entered is invalid, please try again.';
}

http://php.net/manual/en/function.preg-match.php

答案 1 :(得分:0)

使用此网站:http://www.phpliveregex.com/以便将来 preg_matche s