views:

46

answers:

3

i want to restrict certain emails to my website.

an example would be that i only want people with gmail accounts to register to my website.

{
     /* Check if valid email address */
     $regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
             ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
             ."\.([a-z]{2,}){1}$";
     if(!eregi($regex,$subemail)){
        $form->setError($field, "* Email invalid");
     }
     $subemail = stripslashes($subemail);
  }

this is what i have so far to check if its a valid email.

A: 

I suggest you keep an array of regex patterns that you would like the email address to match against. then write a loop to iterate over the array and check the address. whenever a check failed, set some validation flag to false. after the loop, by checking the validation flag you can make sure that the email address is exactly what you want.

farzad
+1  A: 

Don't use a single regex for checking the entire address. Use strrpos() split the address to local part and domain name, check them separately. Domain is easy to check, the local part is almost impossible (and you shouldn't even care about it).

grawity
A: 

How about doing something like:

list(,$domain) = explode('@',$email);
if ($domain != 'gmail.com')
  echo 'not possible to register';
else 
  echo 'Will register';

If you want to validate the email use the filter functions

AntonioCS