Monday, March 15, 2010

Validating Email Addresses

If you need to validate email addresses, eg. from a form post, then the easiest way to do this in coldfusion (7 or later) is to use the isValid function, here is an example:
<cfif NOT isValid('email',emailString)>error!</cfif>

If, however, you need to be a bit more specific with your email validation, such as validate an email for a specific domain then you'll have to do this with a regular expression. Here is the snippet to achieve this:

<cfif NOT isValid("regex", emailString, "^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)*myEmailDomain\.com$")>error!</cfif>

No comments:

Post a Comment