user2158647
user2158647

Reputation: 11

Regex to detect period before @ in email address

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

Answers (3)

Ja͢ck
Ja͢ck

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

jonvuri
jonvuri

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

Daedalus
Daedalus

Reputation: 1667

\.(?=.*@) Should do the trick.

Upvotes: 0

Related Questions