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!


Posted

in

by

Comments

Leave a Reply

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