Reputation: 11
Need regex which will detect [email protected]
but not detect on ([a-z0-9A-Z])@domain.tld
valid: [email protected]
invalid: [email protected]
I recognize a period is legal preceeding @
in an email address but need to detect that condition.
Upvotes: 0
Views: 1374
Reputation: 173562
You don't need regular expressions for this, just compare the position of the period against the at symbol:
// assuming there's already an `@` in $email
if (($p = strpos($email, '.')) !== false && $p < strpos($email, '@')) {
echo 'invalid';
}
If there's no @
in the email address, it will not print 'invalid'
because any integer will not strictly be smaller than false
.
Upvotes: 1
Reputation: 5930
Assuming that you've already validated this as a proper email address:
\..*@
Which means: A .
, then a sequence of 0 or more characters, then a @
Upvotes: 2