HomePHP Page 2 - Email Address Verification with PHP
Validating the proper format of an email address - 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. Alejandro Gervasio explains this approach.
The first step to validating an email address is to check whether it is in the standard format. If you’re a seasoned programmer, feel free to skip over this description. However, for the sake of giving a complete explanation, we’re going to begin by defining the first checking function, which takes advantage of PHP’s built-in support for regular expressions:
Nothing unexpected, right? The simplistic checkEmail() function validates the format of a user’s email address by encoding its standardized format in a regular expression. The preg_match() PHP built-in function looks for matches to the email pattern, given the string $email passed as a parameter. If matches are found, the function will return true. Otherwise, it will return false. It’s a very simple concept, really.
A bit of analysis clearly shows that many invalid email addresses passed as an argument will still match this regular expression. This will result in the function returning true, and considering the addresses as valid data. Though it may be impossible to catch all of the email addresses this way, performing validation routines on the format itself can improve our overall checking process.
Since this function on its own is insufficient for checking whether an email address is valid, we need to look for other ways of improving the validation process. The next step is to check whether an email address corresponds to a real domain by making sure there is a domain registration record for the domain that the user entered. How we achieve that is the subject of the next section.