PHP5 Email Validation

In prior version of PHP you had to use some regex solution to validate an email address, now in PHP5 it’s as simple as:

I’ve been using PHP5 ever since it was released but didn’t look into the PECL Filter extension until recently. Pretty handy.

Please note that I also added the FILTER_SANITIZE_EMAIL filter which removes all illegal e-mail characters from a string. This is not needed if you only want to validate and not clean the input. :)

,

  • http://caramelfrapuccinolovers.blogspot.com dewi was here

    first comment :p
    dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here dewi was here

  • http://goodies.lemonsoftware.eu cristian

    Well, try:

    echo filter_var(filter_var(“John(Doe)@exa\\mple.com”, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);

    still is not a valid email address (wrong domain).

    Note that this these functions only validate the structure of email address or URL strings, they don’t check if the associated domains actually exist.

  • Bill

    @cristian: you’re absolutely right. I was not trying to imply that it checks if a domain exist but I should have been more clear on this. Sorry about that. Also note that even if you check if the domain exists via cURL or method of your choice, it still would not prove it’s a valid working email.

    Would you like me to post an example to check if a domain exists?

  • Pingback: PHP: Check if a URL exists with cURL | Bill Richards()

  • CSSguru

    nice and usefull blog!… keep up the good work.

  • http://www.robsymonds.com Rob

    Just found this and it worked perfectly to replace my deprecated ereg() function. Thanks.

  • Ahmed Ragae

    thanks for this blog but we chose this mail and try it 
    if(isValidEmail(’email@domain’)){  
       echo “mail is valid”;  
    } else {  
       echo “mail is not valid “;
    }

    // the result will be mail is valid 
    //so, its true or false

  • Hm

    helpful

  • Abirami R
  • jackwilson

    Wonderful blog & good post.Its really helpful for me….

    Salesforce Training Institutes in Chennai

  • Jhansijoe

    Thanks for sharing your view to our knowledge’s, its helps
    me plenty keep sharing…Salesforce training center in Chennai

  • N.R. Rao

    Excellent posting. Thanks
    CCNA Training in Pune

  • http://www.bravetechnologies.in Evelin Harriet

    It showing some error. anyway thanks for sharing.

  • ananthi ayiram

    Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things…Really it was an awesome article…very interesting to read..please sharing like this information……
    Web Development Company

Powered by WordPress. Designed by Woo Themes