Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter_var or custom function for email validation in php

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

Thanks!

like image 477
Alex Avatar asked Jul 30 '10 09:07

Alex


People also ask

How to validate an email address PHP?

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.

Which function is used to in email validation?

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.

Which function is used to validate email filters?

The FILTER_VALIDATE_EMAIL filter validates an e-mail address.

What is Filter_var function in PHP?

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.


4 Answers

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.

like image 114
tdammers Avatar answered Oct 05 '22 05:10

tdammers


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

like image 24
Sarfraz Avatar answered Oct 05 '22 05:10

Sarfraz


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.

like image 42
bcosca Avatar answered Oct 05 '22 04:10

bcosca


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.

like image 27
Dominic Sayers Avatar answered Oct 05 '22 03:10

Dominic Sayers