PHP,验证用户是否输入了电子邮件域,不是地址

I was looking for this answer, but I was not able to find this exact variation.

I'm looking for the following to validate:

#1)  @toysrus.com
#2)  @staples.com
#3)  @example.com

And the following NOT to validate:

#1)  staples.com
#2)  Randy@staples.com
#3)  @testcom

Basically, I want to force the user to enter a valid email domain, NOT an email address. Any clue how I could go about this?

I guess alternatively ask a user for their website domain name and just append the '@' character to that, but it's more confusing. It would be easier to simply to ask the user, please enter your company email domain name.

Thanks!

You can use the regular expression to match the email domain name. For example:

$pattern = "^@(\w)+((\.\w+)+)$";
if (preg_match($pattern,$domain))
    echo "Domain name right"

"^@" means the string should start with "@"."(\w)+" match at least 1 number or letter."(.\w+)+" match at least 1 domain name,such as ".com" or ".edu.cn". "$" means the end of the string.

This can be done in a multi-stage process

$email = 'something@somedomain.com';
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // The email is a valid format so now we check the domain
    $domain = explode('@', $email);
    if(getmxrr($domain[1])) {
        // This is about as far as we can take it.
        // The email is valid and the domain has MX records
    }
}