Home arrow PHP arrow Page 4 - Building a PHP5 Form Processor: Coding the Form Validator Module

The big challenge: checking email addresses with the “validateEmail()” method - PHP

In this second part of a three-part series, we take up the topic of server-side validation. By the time you finish this article, you'll have the general guidelines for how to build a form validating class. You'll use some PHP built-in introspection functions, along with regular expressions, to assist you in building this class.

  1. Building a PHP5 Form Processor: Coding the Form Validator Module
  2. Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  3. Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  4. The big challenge: checking email addresses with the “validateEmail()” method
  5. Checking for errors: defining the “checkErrors()” and displayErrors()” methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
January 23, 2006

print this article



For anyone who has spent a long time writing validation routines, verifying email addresses is a challenging task. In the good old days, a robust email pattern coded in a regular expression was, most of the time, good enough to make sure that a user had entered a well-formed email address. Now, things are different, considering the vast number of current email domains. However, in order to keep the method’s source code simple and readable, I’ll walk through an intermediate example, and instruct the method to check only for well-formed addresses and existing domains in the DNS. Its signature is as follows:

public function validateEmail($field,$errorMessage){


As shown above, the method verifies whether the supplied email address is at least well-formed, without troubling things too much with complex patterns. It then searches in the DNS for the corresponding MX records, in order to determine whether the domain part corresponds to a real email domain. To perform this task, I use the “checkdnsrr()” PHP built-in function, which is extremely helpful when searching DNS records. Of course, this fact doesn’t mean that the user really exists, but it’s a decent way to check for the existence of a given email domain.

From this point onward, you can make your own way either by improving the effectiveness of this method or writing a full-fledged alternative solution, in order to verify user-supplied email addresses.

Now, in case you don’t know, the PHP “checkdnsrr()” function isn’t available on Windows systems, so here’s the alternative “windnsrr()”function, which essentially performs the same task on Microsoft servers:

private function windnsrr($hostName,$recType=''){
        exec("nslookup -type=$recType $hostName",$result);
        foreach($result as $line){
                return true;
        return false;
    return false;

And eventually, if you’re running your PHP applications on a Windows server, the “validateEmail()” method should be rewritten like this:

public function validateEmail($field,$errorMessage){

After having defined the last checking method of the class, the only thing left to complete the definition of the form “validator” class consists of including two useful corollaries: the “checkErrors()” and “displayErrors()” methods.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: