Whenever it comes time to validate an email address, I always end up going through the same exact process.
I start with a simple regular expression— for example, this:
It works great for a majority of email addresses— however, it is not perfect. It lets through a lot of invalid email addresses. For example, ".firstname.lastname@example.org" is considered a valid email address by this regular expression. So, let's take a few steps to correct this:
A bit better, right? But what about valid email addresses, such as email@example.com and $A12345@example.mobile? Same goes for the seemingly invalid email addresses such as "Abc\@def"@example.com. But, alas, these are all valid.
No, I am not going to even attempt a regex that accommodates these. Others have tried. However, it is futile; email validation ends up either being too strict with what it rejects, or too liberal with what it lets through. Or, in many cases— both.
But for the sake of argument, let's assume we do create a regex that follows the RFC perfectly. That still doesn't stop people from getting through with something like firstname.lastname@example.org. Sure, we could even go as far as checking to make sure it is a real, actual email address. But even then, there is no stopping email@example.com— an email address that surely exists, however probably is not owned by the person filling out the form.
Well, what about this one:
Here's the secret. This is not a programming problem- it's a usability issue. If the user benefits from giving you their real email address, they will. If not, you should not be asking for an email address.
It is that simple, really. Just make sure they at least attempted an email address (otherwise, they may mix up the fields), and throw in a good reason to hand it over- and you will never have a problem.
About Gregory Koberger
« Newer Article
PHP: MVC By Nature?
After spending hours upon hours fighting with frameworks, I got fed up. They are certainly tempting, since they take care of the initial structuring of your program.
Older Article »
What Kind of Company Are You?
The biggest problem inside companies is self interest. The sales team wants to make money, the writers wants journalistic integrity, the legal team wants to avoid lawsuits, the engineering team wants to use cool technology— the list goes on and on.