PHP: Simple Email Verification Function
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 :
Or in a single function :
Hope you enjoy it!
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!