Which solution is better, using the built in validation filter_var('email', FILTER_VALIDATE_EMAIL)
or a custom function?
Thanks!
Which solution is better, using the built in validation filter_var('email', FILTER_VALIDATE_EMAIL)
or a custom function?
Thanks!
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
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.
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.