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 = "myemail@yahoo.com";

//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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.