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 ?
posted on 01.06.2019 at 13:46
0pnshow more

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.
posted on 01.06.2019 at 18:25
0pnshow more
share on facebookshare on twitter
2020 AnswerTabsTermsContact us
This site uses cookies to improve your experience, for analytics and ads. By continuing to browse this site you agree to our use of cookies.