shinytoygun Posted April 18, 2011 Share Posted April 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234009-using-preg_match-to-validate-my-form/ Share on other sites More sharing options...
QuickOldCar Posted April 18, 2011 Share Posted April 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234009-using-preg_match-to-validate-my-form/#findComment-1202901 Share on other sites More sharing options...
shinytoygun Posted April 21, 2011 Author Share Posted April 21, 2011 Thanks, I'm going to try to use this Quote Link to comment https://forums.phpfreaks.com/topic/234009-using-preg_match-to-validate-my-form/#findComment-1204315 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.