正则表达式特定电子邮件域(preg_match)

时间:2018-05-03 00:01:24

标签: php regex preg-match

我是正则表达式的新手,需要一些帮助。我需要使用正则表达式来验证电子邮件,以便它有一个特定的电子邮件,所以只有以@ School.edu结尾的电子邮件,截至目前我还有

    $username = $_REQUEST["username"];
    $password = $_REQUEST["pass"];
    $repeatP = $_REQUEST["repeat"];
    //Username is Acceptable, Passwords Match and Appropirate Size
    if(preg_match("/^[a-zA-Z0-9]+\oneonta.edu$/i", $username)) {
        $userCheck = true; 
        ?> <p>Username is Acceptable</p>
        <?
    }
    else {
        ?><p>Error: Username is Unacceptable, Please go back and try again</p> 
        <?
    }

我使用School.edu作为我的学校edu的一个例子,我只是不知道如何只允许School.edu电子邮件

1 个答案:

答案 0 :(得分:1)

一个选项:

$mystring = '@oneonta.edu';
$pos = strpos($mystring, $_REQUEST["username"]);

if ($pos !== false) {
     echo "<p>Username is Acceptable</p>";
} else {
    echo "<p>Error: Username is Unacceptable, Please go back and try again</p> ";
}

另一个

 if(substr($_REQUEST["username"], -12)=='@oneonta.edu'){
     echo "<p>Username is Acceptable</p>";
}else{
    echo "<p>Error: Username is Unacceptable, Please go back and try again</p> ";
}

更多?

   $pieces = explode("@", $_REQUEST["username"]);

     if($pieces[1]=='oneonta.edu'){
         echo "<p>Username is Acceptable</p>";
    }else{
        echo "<p>Error: Username is Unacceptable, Please go back and try again</p> ";
    }