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 ?
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.
1 answer