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
https://forums.phpfreaks.com/topic/234009-using-preg_match-to-validate-my-form/
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('[email protected]')."<br />";
echo checkEmails('[email protected]')."<br />";
echo checkEmails('[email protected]')."<br />";

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

$email2 = "[email protected]";
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.

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.