0

i want to validate the email address domain using pregmatch. also the valid edu domain i inserted in email list array so when user enter the email address that entry first check in email list array. if it is available then it is validate. i am doing validation part on server side.. any help is appericiated. thanks in advanced...

<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("@",$email); // split email 
$email_name = $email_exp[1]; // get the domain of email address

$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu"); // valid edu domain 

for($i=0;$i<sizeof($email_list);$i++)
{
if(in_array($email_name,$email_list))
{
if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_name))
{
  // validate email 
}
}
}
prashant
  • 3
  • 3
  • Why not [**`filter_var($email_name, FILTER_VALIDATE_EMAIL)`**](http://php.net/manual/en/filter.filters.validate.php) – Prix Dec 21 '13 at 06:14
  • if am using filter_var i don't need preg_match here.. ?? @stoic – prashant Dec 21 '13 at 06:18
  • @stoic i also need specific edu domain from listed in arrray so FILTER_VALIDATE_EMAIL replace from my email_list array... ??? – prashant Dec 21 '13 at 06:29

1 Answers1

1

Use filter_var, and replace the preg_match call with it.

if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE) {
   // email is valid
}

So, the updated code will be:

<?php
$email = $_POST['email']; // get the email value
$email_exp = explode("@",$email); // split email 
$email_name = $email_exp[1]; // get the domain of email address
$email_list = array("berkely.edu","ucfs.edu","udef.edu","ucms.edu","ucef.edu");

$email_is_valid = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == TRUE;
if($email_is_valid && in_array($email_name,$email_list) ) {
  // email is valid for your purposes
}
Prix
  • 19,417
  • 15
  • 73
  • 132
Stoic
  • 10,536
  • 6
  • 41
  • 60
  • after using filter_var i don't need the preg match here... ?? – prashant Dec 21 '13 at 06:17
  • exactly. `filter_var` validates the email for you, and therefore, there is no need for `preg_match` – Stoic Dec 21 '13 at 06:18
  • user enter email domain also checking in email list array .. if it is available in it then it's validate... FILTER_VALIDATE_EMAIL REPLACE FROM my $email_list – prashant Dec 21 '13 at 06:21
  • @prashant: updated the answer with the correct code. – Stoic Dec 21 '13 at 06:25
  • ok i am replace the code that u provide me.. and checked it's result... – prashant Dec 21 '13 at 06:39
  • it throw me 500 internal server error don't know why... ??? – prashant Dec 21 '13 at 06:43
  • @prashant there was an extra `)` at the `$email_is_valid` line, check again the above example and compare with yours. – Prix Dec 21 '13 at 06:47
  • i don't know why we use FILTER_VALIDATE_EMAIL WHAT it's actual working.. because my actual requirement is checking that user enter the email i am checkin in my list_array that no of edu domain i want to validate.... if i passed in_array parameter only in if condition then only it working for me... @prix – prashant Dec 21 '13 at 07:02
  • my actual question was how to validate edu domain using preg match and my register edu domain is specific that i am inserted into one array list.... if u saw my original code i used the preg_match.... my validation is server side... so can i implement using preg_match for my register edu domain @prix.. – prashant Dec 21 '13 at 07:05
  • @prashant Stoic's answer does exactly what you said, it will check the email is a valid email and then check if it ends with one of the given domains you have on your array. If you want to know more about what filter_var does, you can read it on the link link I have posted on the comments of your question. – Prix Dec 21 '13 at 07:14