Categories
PHP

Validating An Email Address in PHP 5

In PHP 5.2 Zend has added a function called filter_var(). It validates a lot of things for you without having to write any regular expressions or anything else. You do it like this:

if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL) !== false) {
    print 'valid email';
} else {
    print 'Invalid email';
}

filter_var()returns the value back if its valid or false if it is not. And, it doesn’t just validate email addresses. It can validate booleans, floats, etc and sanitize data too.

Here are some links that will be helpful:

http://us2.php.net/manual/en/filter.filters.validate.php
http://us2.php.net/manual/en/function.filter-var.php

Enjoy!

By Moazzam

Moazzam is a software developer working in the big apple. He has over a decade of experience in development. While most of his work involves developing web applications, he does development for Android and Windows Mobile in his spare time.

Leave a Reply

Your email address will not be published. Required fields are marked *