To prevent 2 consecutive dots (.
) use negative lookahead (?!.*?\.\.)
if (!preg_match('/^(?!.*?\.\.)[a-z0-9_.]+@[a-z0-9.-]+\.[a-z]+$/im', $email))
return("Invalid email address");
EXPLANATION:
^ # Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
(?! # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
. # Match any single character that is NOT a line break character (line feed)
*? # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\. # Match the character “.” literally
\. # Match the character “.” literally
)
[a-z0-9_.] # Match a single character present in the list below
# A character in the range between “a” and “z” (case insensitive)
# A character in the range between “0” and “9”
# A single character from the list “_.”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
@ # Match the character “@” literally
[A-Z0-9.-] # Match a single character present in the list below
# A character in the range between “A” and “Z” (case insensitive)
# A character in the range between “0” and “9”
# The literal character “.”
# The literal character “-”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\. # Match the character “.” literally
[A-Z] # Match a single character in the range between “A” and “Z” (case insensitive)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\$ # Assert position at the end of a line (at the end of the string or before a line break character) (line feed)