Search code examples
regexemail-validation

block specific type of email address in php via regex


I use the following regex pattern for validating the email address that works fine.

/^[^\@]+@.*\.[a-z]{2,6}$/i

But the problem is I want to generate error if email like [email protected] are enter. Actually all those emails are invalid which have characters same as before & after **.** like [email protected] invalid, [email protected] invalid


Solution

  • You can use a back-reference in a regexp to test if two parts are the same. In PHP you'd write:

    if (preg_match('/^(\w+)\.\1@.*/', $email)) {
        echo "That's a spammy name";
    }