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:
<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:
<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:
<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!
see: http://en.wikipedia.org/wiki/E-mail_address for a list of valid characters
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.