Regex pattern - If email address is not valid / does not match

I want to check in PHP if email address, that is entered by user is not correct.

To validate if email address is correct I used:
preg_match("/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,6})$/", $email)

Now I need to create a condition, to check if email address does not match the pattern. Do you have any suggestions ?
01 Jun 2019 at 01:46 PM
0give a positive ratinggive a negative ratingshow more
zeddzedd
Hi,

To check if email address is not valid, you can use this:

if ( preg_match("/^(?![a-zA-Z0-9\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,6}$).*/", $email) ) {
echo "Email address is not valid";
}


The ?! creates a negative lookahead in pattern.
01 Jun 2019 at 06:25 PM
0give a positive ratinggive a negative ratingshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook
2022 AnswerTabsTermsContact us