A few weeks ago I learned about a great Gmail feature that allows you to filter your email with personalized email addresses using a plus sign. For example, if you think you’ll be spammed by signing up for something like a forum, you could sign up with the email username+forum@gmail.com and setup a filter to automatically mark those messages as spam.

Both of my current projects at Sproutbox and the Eppley Institute involve setting up or testing user systems. To do this, I make a lot of dummie test accounts but these accounts need to have valid email addresses to test things like password recovery. Instead of racking my brain for every email address I have ever created, I started registering test accounts with emails such as username+tester1@gmail.com.

This was working great until today in Joomla my email addresses weren’t validating with their JavaScript validation script. Luckily, it was easy to see how they were validating email addresses.

scrnr-validateInside the validate.js script was a small function that used a regular expression to make sure entered email addresses were in the traditional form of Username09@Domain1.com.

this.setHandler('email',
function (value) {
regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(value);
});

If you’ve never seen or used regular expressions before, they are very powerful and awesome but can be overwhelming at first. All that expression is saying – “Look at the beginning of the line and make sure there are only upper and lowercase letters, numbers zero though nine, periods, underscores, and dashes before at least one at sign.” The rest of it goes the same way.

So to allow plus signs in the first part of the email address you just add a ‘+’ after the ‘-’ and before the ‘]’.

regex=/^[a-zA-Z0-9._-+]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

Now it will allow email addresses that look like Username90+whatever@Domain1.com.

If you’re a developer and want to be nice to your users, make sure to remember to add this ‘+’ in your form validation scripts so that soon-to-be Gmail ninjas like me can filter your spam newsletters accordingly.