Validating email address in php

Posted by / 21-Aug-2016 03:20

Validating email address in php

Many applications in the field of Web development need to validate email addresses.While this can be done in a variety of ways, one simple but effective way involves writing your own functions in PHP. Within the huge and fascinating field of Web development, one of the most common tasks that many applications have to deal with is, undoubtedly, verifying whether a user email address is valid.The third option involves writing our own set of functions for in-deep email address checking, which can be considered an intermediate solution between the two above described.This approach is versatile and portable enough to be used whether we want to expand basic validating functions or add extra functionality to existing classes.A very quick snippet today because I've told two people to use this approach in the last few days and both of them told me they didn't know about it.How to check if an email address is valid in PHP: use one of the Filter functions, like this: $email1 = "[email protected]"; // not a valid email $email2 = "[email protected]"; // valid email $clean_email1 = filter_var($email1, FILTER_VALIDATE_EMAIL); // $clean_email1 = false $clean_email2 = filter_var($email2, FILTER_VALIDATE_EMAIL); // $clean_email2 = [email protected] The Filter extension was new in PHP 5.2, but is one of the unsung heroes of the language.It's rare for me to ever describe one approach as the "right" way to do something - but for validating data, Filter really is excellent, offering both validating and sanitising filters and generally making it super-easy to clean up incoming variables.

Several approaches can be taken to address the problem, depending on the level of complexity desired for the validation itself.

If the application is going to make use of a basic level of validation, a quick-and-dirty way to handle the situation might be to implement a simple PHP function that performs pattern matching to a standardized email address format, as we have seen many times.

However, when a deeper and more complex validation is required, we should take a look at well-trusted validation classes, such as Pear’s HTML_Quick Form class, or many other validation classes widely available out there.

Certainly, this should sound very familiar to most Web developers, whether they are setting up their first consciously-coded script or implementing full-blown applications required to handle more complex processes.

Whatever the case, validating a visitor’s email address to see if it belongs to a real domain is always a good step to help you avoid, at least partially, several possible problems that arise when applications are receiving incoming bogus data.

validating email address in php-22validating email address in php-46validating email address in php-71

Given such qualities, it isn't a surprise that email has become a defacto tool of business for communicating with its clientele.

One thought on “validating email address in php”