Lists, Email Addresses, Regular Expressions

In belated celebration of the newly declared "Regular Expression Day", I thought I would cover just one of the many way in which regular expressions makes my life easier.

When I read Ben's entry and saw that he uses regular expressions every week, I thought "I don't use them that often". Within in hour of that thought, however, I was on the phone with a fellow programmer discussing a problem for which regular expressions was a great solution.

In this case, the users of a site need to be able to enter multiple email addresses to receive email for certain types of events (I don't know the details). The site has a form that allows them to enter multiple email addresses.

The instructions state that the email addresses must be separated by commas. You can probably see the problem here.

That's right, sometimes they didn't follow the directions. Consequently, we sometimes got failed email.

The solution was to allow them to separate the email addresses however they want.

We added this code in the component:

<!--- If characters are not part of an email address, replace them with a comma to create a valid list --->
<cfset arguments.Emails = REReplace(arguments.Emails,"[^a-zA-Z0-9_\-\.@]+",",","all")>

The ensures that any characters that aren't a valid part of an email address are treated as a list delimiter and changed to a comma.

We also decided to check for invalid email addresses in the list:

<!--- Throw an error for any invalid email address --->
<cfloop list="#arguments.Emails#" index="Email">
   <cfif NOT REFindNoCase("^['_a-z0-9-]+(\.['_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name|jobs|travel))$",Email)>
      <cfthrow type="EmailFormat" message="The email address: '#Email#' is invalid.">
   </cfif>
</cfloop>

This code is running on ColdFusion MX 6.1. If it had been on ColdFusion 7 or better, it would have been even easier:

<!--- Throw an error for any invalid email address --->
<cfloop list="#arguments.Emails#" index="Email">
   <cfif NOT isValid("email",Email)>
      <cfthrow type="EmailFormat" message="The email address: '#Email#' is invalid.">
   </cfif>
</cfloop>

Either way, we catch the exception in our form (in our case, using the sebForm's CatchErrTypes) and display the error to the user.

Next on the list, using ListChangeDelims() to display the email addresses each in their own line instead of as a comma delimited list for readability.

Just one of many examples of how regular expressions make life easier. Thanks for the reminder Ben!

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
your first regex is probably too simplistic. although it will do for most cases, what happens when it gets something like john.smith+sebtools@gmail.com? it'll turn it into john.smith,sebtools@gmail.com.
see: http://en.wikipedia.org/wiki/E-mail_address for a list of valid characters
# Posted By duncan | 6/5/08 12:20 AM
Duncan,

Thanks! I suspected someone more knowledgeable might have a suggestion on how to improve the regex. We will certainly expand based on that reference.

Thanks for the help.
# Posted By Steve Bryant | 6/5/08 5:53 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.