views:

40

answers:

4

Which solution is better, using the built in validation filter_var('email', FILTER_VALIDATE_EMAIL) or a custom function?

Thanks!

+1  A: 

Filter var for email remove all characters, except letters, digits and !#$%&'*+-/=?^_`{|}~@.[]. Now it is up to your whether you want to go with this filtration or create a custom solution.

And here is an excellent article on that:

Input Validation: Using filter_var() Over Regular Expressions

Sarfraz
The *santizing* filter does, but not the *validating* filter. Without being sanitized, a "valid" email (according to the filter) can still contain characters other than those listed above.
salathe
+2  A: 

Custom validation gives you more control over how far you want to go with this. What is and is not valid as an e-mail address is more complex than you might think, and most of the time, it is better to be too lax with this than too strict. After all, a syntactically valid e-mail address doesn't guarantee that the account actually exists, let alone that it's being actively used. Something like, it must contain one @, at least one dot after the @, at least one character before the @, and none of the illegal characters, is probably good enough in most cases.

tdammers
PHP does not validate email addresses according to the official RFC (I can't find a reference for this right now, but it came up here recently). Even though `FILTER_VALIDATE_EMAIL` is probably good enough most of the time, I'd recommend against it and either go with an RFC compliant checker or, as you said, take it easy.
deceze
Oh, here we go: http://svn.php.net/repository/php/php-src/branches/PHP_5_3/ext/filter/logical_filters.c Look for the regex in `php_filter_validate_email`. It's much too short to be fully RFC compliant. :)
deceze
My point exactly. RFC compliant doesn't mean it exists and works. Also, I'd be surprised to hear that all mail agents are RFC compliant.
tdammers
+2  A: 

PHP's filter_var might be satisfactory for most applications, but if you want to compare performance and validity, look at this site http://www.linuxjournal.com/article/9585 to understand what RFC 2822-compliance means.

stillstanding
A: 

I found this while Googling, Hope this explains you better

http://www.addedbytes.com/code/email-address-validation/

Chetan sharma