Validate an email address with php

php-sniplets Add comments

This little function with regex is used for Email Address Verification – short and simple, but powerfull, i use it really often. Validating the proper format of an email address is handy, so your users cant type in invalid adresses and you dont have databases full of wrong email adresses.

Email addresses always have a standard format. We can check those with regular expressions:

function validateEmail($email) {
if (!preg_match(“/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/” , $email)) {
return false;
}
return true;
}

You can now check your input data:

$email = $_POST['email'];
if (validateEmail($email)) {
proceed();
} else {
echo ‘false’;
}

Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in