Form and Spelling Validation -
(Page 6 of 14 )
Validating email addresses can be tricky business. They come in many different forms and lengths. Luckily, we have regular expressions on our side so we should be able to differentiate between good and bad email addresses. Before we look at the function we will use to validate email addresses, let's take a look at some common forms of them so we know what we are dealing with.
name@somwhere.com first.last@somewhere.com first.last@place.someplace.us my_name@me.info me.something@somewhere.co.uk |
As you can see, email addresses can come in all shapes and sizes. Now, let's look at the function, and then we will examine the regular expression piece by piece.
<?php function checkEmail($email) { $pattern = "/^[A-z0-9\._-]+" . "@" . "[A-z0-9][A-z0-9-]*" . "(\.[A-z0-9_-]+)*" . "\.([A-z]{2,6})$/"; return preg_match ($pattern, $email); } ?> |
Now, let's take a closer look at the pattern line by line. The first line specifies the match should begin at the start of the string. Then, it matches any alphabetic character, any digit, a period, underscore, or a hyphen. These characters must occur at least one time, but may appear many times. That takes care of the user name portion of the email address. The next line matches the literal @ character and nothing else. The third line starts matching the host name part of the email address. First, it matches any alphabetic character or digit exactly once. Then, any alphabetic character, any digit, or a hyphen can be matched zero or more times.
The fourth line starts by matching a period. It then goes on to match any alphabetic character, a digit, underscore, or hyphen at least once. This entire fourth line can be matched zero or more times.
The last line of the pattern matches a period followed by a set of any alphabetic characters. The set should have between two and six characters. This will match the top-level domain, i.e., .com, .net, .uk, etc. At the time of this writing, all top-level domains are between two and six characters in length. With the addition of new top-level domains, however, this could change. Simply adjusting the range will take care of this problem should it occur.
Next: >>
More Miscellaneous Articles
More By Matt Wade