PHP: Simple Email Verification Function

Here we will give you code snippets or tips related to PHP/MySQL/XHTML/CSS/JavaScript/AJAX development
Forum rules
Be civilized... # Seja civilizado... # Être civilisé...

PHP: Simple Email Verification Function

Postby weblivehelp on Fri Aug 01, 2008 2:22 pm

Hey there,

This month we'll provide you with a very simple yet super-useful email verification function, using regex (regular expressions)

It is based on the function you can find here: http://php.snippetdb.com/view.php?ID=43 but we corrected a little error, for tld's like .com.pt or such (bigger than 4 characters in length, up to 6)

We are going to provide that in two ways:

Using a general validation function :

Code: Select all
function isValid($type,$var) {
   switch ($type) {
      case 'email' : {
         if (eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $var)) {
            return true;
         } else {
            return false;
         }
      }break;
      //-- Here you can add more cases for phone validation or other types of validations
      default : return false;
   }
   return false;
}

//-- Usage:
$email = "info@weblivehelp.net";
if (isValid('email',$email)) {
   echo "Email is valid!";//-- Output: Email is valid!
} else {
   echo "Email is NOT valid!";//-- Output: Email is NOT valid!
}


Or in a single function :

Code: Select all
function isValidEmail ($email) {
if (eregi("^[a-z0-9._-]+@[a-z0-9._-]+.[a-z]{2,6}$", $email)) {
   return true;
} else {
   return false;
}

//-- Usage:
$email = "info@weblivehelp.net";
if (isValidEmail($email)) {
   echo "Email is valid!";//-- Output: Email is valid!
} else {
   echo "Email is NOT valid!";//-- Output: Email is NOT valid!
}


Hope you enjoy it! :)
weblivehelp
Site Admin
 
Posts: 9
Joined: Thu May 01, 2008 3:23 pm

Return to Web Development

Who is online

Users browsing this forum: No registered users and 2 guests

cron