Jump to content

Using preg_match to validate my form


shinytoygun

Recommended Posts

Hey Everyone,

 

Im having trouble with this code, i'm trying to use preg_match to display an error when someone inputs their email and it doesnt have a specific domain (like for example yahoo.com). My logic is to use it as a filter, if the input doesnt have the word '@yahoo.com' it will show the error. What am I doing wrong?

 

if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\yahoo.com", $data['email']) === 0)
		$err .= "• $lang[ERROR_DOMAIN]<br>";

 

Any help will be greatly appreciated.

 

Thanks.

 

- STG

Link to comment
Share on other sites

Here's the way I do it. Use it in any way you desire.

 

<?php

function checkEmails($email_address){
if ($email_address != '') {
$domain = end(explode("@", strtolower(trim($email_address))));
$good_emails = array('yahoo.com','aol.com','live.com','hotmail.com','gmail.com');

if (in_array($domain, $good_emails)){
return "$email_address accepted";
return True;
} else {
return "$email_address not accepted";
return False;
}
} else {
return "Email is empty";
return False;
}
}

//usage
echo checkEmails('bobby@yahoo.com')."<br />";
echo checkEmails('mary@mooomail.com')."<br />";
echo checkEmails('mary@live.com')."<br />";

$email1 = "";
echo checkEmails($email1)."<br />";

$email2 = "gsgaarg@spammail.com";
echo checkEmails($email2)."<br />";

if(checkEmails($email1) == True){
echo "do whatever you want here";
}

?>

 

 

You can even expand onto this and do a rejected full emails array.

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.