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 ?
0
give a positive ratinggive a negative rating
01 Jun 2019 at 01:46 PM
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.
0
give a positive ratinggive a negative rating
01 Jun 2019 at 06:25 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us