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’;
}

4 Responses to “Validate an email address with php”

  1. eduardo aubert Says:

    soy de argentina provincia de entre rios ciudad de urdinarrain c p 2826/ exelentes paginas de ciencias deceo tener contactos para escuelas y colegios materiales gracias eduardo aubert 19/10/07

  2. Sanjeev Says:

    well do you have any idea about how to check the email address by sending some smtp request or other request to validate that particular email address exists

    Sanjeev

  3. Handy Says:

    Thank you for this code. It is really small. But I think it is easier to validate the email with Javascript, because it increases the usability of the application. If a person wants to fake the email, there is no way to prevent this, at least by giving you a valid not existing email.

  4. Ingo B. Says:

    @Sanjeev
    I use a special calss to validate email with SMTP. Sometihing like this: http://www.tutorials.de/forum/php-tutorials/231820-emails-ueber-einen-beliebigen-server-verschicken.html or PHPMailer

Leave a Reply

Entries RSS Comments RSS Login