Jump to content

Validating email with regular expressions(preg_match)


borisboris

Recommended Posts

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>";

}

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})$~";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.