Not yet rated

Problem

A ColdFusion application requires a valid email address be entered in a form.

Solution

ColdFusion allows the use of Regular Expressions to do pattern matching against a string with the REFind and REFindNoCase functions. Using the REFindNoCase function will allow an application to determine if a string is a valid email address. The IsValid function can also be used to accomplish this task.

Detailed explanation

Using the REFindNoCase method is very simple.  The more difficult part is selecting the Regular Expression to use.  For this example the following Regular Expression will be used:

"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$"

Using the Regular Expression to validate an value entered into a form field is very straighforward:

<cfif REFindNoCase(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$",
form.email  ) >
Valid Email Address
<cfelse>
Invalid Email Address
</cfif>

There are numerous ways to validate an email address in ColdFusion and using a Regular Expression is
only one way.  The IsValid() function will also validate email addresses but can produce irregular
results at times. 

<cfif IsValid(  "email", form.email )>
Valid Email
<cfelse>
Invalid Email
</cfif>

However, the IsValid function also allows for use of Regular Expressions.

<cfif IsValid(  "regex", form.email,
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$"
)>
Valid Email
<cfelse>
Invalid Email
</cfif>

ColdFusion offers numerous easy and simple to use ways to determine if a email address is valid.

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes