A ColdFusion application requires a valid email address be entered in a form.
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.
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.
+