Which solution is better, using the built in validation filter_var('email', FILTER_VALIDATE_EMAIL)
or a custom function?
Thanks!
You can perform a PHP validation email by using the filter_var() function and passing the given email and filter id “FILTER_VALIDATE_EMAIL” as arguments. The stated filter id will check if the format of the email is correct according to the syntax in RFC 822.
PHP - Validate E-mail The easiest and safest way to check whether an email address is well-formed is to use PHP's filter_var() function.
The FILTER_VALIDATE_EMAIL filter validates an e-mail address.
filter_var() is a PHP function used to filters a variable with the help of a specified filter. In PHP programming language we can use filter_var() function to validate and sanitize a data such as email id, IP address etc.
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.
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
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.
You are welcome to use my free PHP function is_email()
to validate addresses. It's available here.
It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists.
You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: http://isemail.info/about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With