borisboris Posted July 19, 2011 Share Posted July 19, 2011 I'm a PHP newbie. I have the following code but cannot get it to work, any ideas on how to make this code work: $email = "[email protected]"; //I would like to use the variable $good_email to hold the regular expression //also I need to use the specific below pattern but I'm probably missing double quotes or single quotes i'm //guessing $good_email = ([a-zA-Z0-9]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)+"; if (preg_match(/"$good_email"/, $email)) { echo $email." is a valid email address.<br>"; } Link to comment https://forums.phpfreaks.com/topic/242395-validating-email-with-regular-expressionspreg_match/ Share on other sites More sharing options...
AyKay47 Posted July 20, 2011 Share Posted July 20, 2011 yeah you have the syntax a little off here, not a big deal... $good_email = "~^([a-zA-Z0-9]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)$~"; if (preg_match($good_email, $email)) { echo $email." is a valid email address.<br>"; } however this pattern will allow for non-emails... perhaps something like this instead for the pattern.. $good_email = "~^([a-zA-Z0-9]+@[a-zA-Z0-9_-]+\.[a-z]{2,3})$~"; Link to comment https://forums.phpfreaks.com/topic/242395-validating-email-with-regular-expressionspreg_match/#findComment-1244968 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.