ColdFusion CustomTags

E-mail address validate

Use this custom tag to check if the e-mail address contains valid characters, syntax and length.
Notice: The tag dos not check if the entered e-mail address is valid, only the syntax.

<cfparam name="Attributes.email" type="string">
<cfset caller.emailmsg = ''>
<cfset valid = True>
<cfset chr_list =
'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,@,.,-,_,+,~'>
<cfset email_length = Len(Attributes.email)>
<!--- Check if 'E-mail' has been provided. --->
<cfif Len(Attributes.email) IS 0>
<cfset caller.emailmsg = 'E-Mail address is required.'>
<cfset valid = False>
</cfif>
<!--- Check if length of E-mail is less than 6 characters --->
<cfif valid>
<cfif Len(Attributes.email) LT 6>
<cfset caller.emailmsg = 'E-mail must be at least 6 characters.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check for invalid character(s) in the E-mail --->
<cfif valid>
<cfloop index="counter" from ="1" to = "#email_length#">
<cfset chr_check = Mid(Attributes.email, counter, 1)>
<cfif NOT ListContainsNoCase(chr_list,chr_check)>
<cfset caller.emailmsg = 'E-mail contains a invalid character.'>
<cfset valid = False>
</cfif>
</cfloop>
</cfif>
<!--- Check if the e-mail contains '@' --->
<cfif valid>
<cfif Attributes.email DOES NOT CONTAIN '@'>
<cfset caller.emailmsg = 'E-mail doesn''t contain a ''@''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if the e-mail begins with a '@' --->
<cfif valid>
<cfif Left(Attributes.email,1) IS '@'>
<cfset caller.emailmsg = 'E-mail can''t begin with a ''@''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if the e-mail end with a '@' --->
<cfif valid>
<cfif Right(Attributes.email,1) IS '@'>
<cfset caller.emailmsg = 'E-mail can''t end with a ''@''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if it's more than one '@' in the e-mail --->
<cfif valid>
<cfif findnocase("@",Attributes.email,(findnocase("@",Attributes.email)+1)) IS NOT 0>
<cfset caller.emailmsg = 'E-mail may only contain one ''@''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if it's atleast 4 characters after the '@' in the e-mail --->
<cfif valid>
<cfif Len(ListLast(Attributes.email,'@')) LT 4>
<cfset caller.emailmsg = 'E-mail must have at least 4 characters after the ''@'' sign.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if e-mail contain '.' --->
<cfif valid>
<cfif Attributes.email DOES NOT CONTAIN '.'>
<cfset caller.emailmsg = 'E-mail doesn''t contain a ''.''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if the e-mail begins with a '.' --->
<cfif valid>
<cfif Left(Attributes.email,1) IS '.'>
<cfset caller.emailmsg = 'E-mail can''t begin with a ''.''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if the e-mail ends with a '.' --->
<cfif valid>
<cfif Right(Attributes.email,1) IS '.'>
<cfset caller.emailmsg = 'E-mail can''t end with a ''.''.'>
<cfset valid = False>
</cfif>
</cfif>
<!--- Check if the domain name of the e-mail is atleast 2 characters --->
<cfif valid>
<cfif Len(ListLast(Attributes.email,'.')) LT 2>
<cfset caller.emailmsg = 'The domain of the E-mail address must be at least 2 characters.'>
<cfset valid = False>
</cfif>
</cfif>

Example:
(here we use cfmodule to call the custom tag)

<!--- Start checking the form. --->
<cfset valid = True>
<cfset errormsg = "">

<!--- Check if the 'e-mail' address is valid. --->
<cfmodule template="../customtags/emailvalidate.cfm"
email = "#Form.e_mail#">

 

<cfif Len(#emailmsg#) NEQ 0>
<cfset valid = False>
<cfset errormsg = errormsg & "<li>#emailmsg#<br>">
</cfif>

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.