【问题标题】:validation, what am i doing wrong?验证,我做错了什么?
【发布时间】:2016-11-07 15:13:55
【问题描述】:

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

    //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 “您输入的电子邮件无效,请重试。”。谁能为我编辑代码并指出我做错了什么?谢谢。

【问题讨论】:

标签: php validation email


【解决方案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

【讨论】:

  • 天哪,我试图使用 preg_match 但我从来没有放过“!”在此之前,我想知道为什么它会通过所有密码!对不起这篇文章......谢谢你的回答!!!
【解决方案2】:

使用此网站:http://www.phpliveregex.com/ 供将来使用 preg_matches

【讨论】:

  • 将此作为评论发布。这不是答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2021-04-14
  • 2021-04-19
相关资源
最近更新 更多