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>

Saturday, March 13, 2010

Convert Upper ACSII Characters

This snippet will convert characters with an ascii value between 128 and 255 to their lower ascii equivalent. This can be useful for converting strings to be used in urls, eg: www.website.com/littéraire is an invalid url since it contains the letter 'é'. To make it valid the 'é' must be converted to its lower ascii equivalent; 'e'.

Here is the code to do this:


<cfset string = "Your text with upper ascii characters ééé">
<cfset parsedText = replaceList(string,"À,Á,Â,Ã,Ä,Å,à,á,â,ã,ä,å","A,A,A,A,A,A,a,a,a,a,a,a")>
<cfset parsedText = replaceList(parsedText,"Æ,æ,Ç,ç","AE,ae,C,c")>
<cfset parsedText = replaceList(parsedText,"È,É,Ê,Ë,è,é,ê,ë","E,E,E,E,e,e,e,e")>
<cfset parsedText = replaceList(parsedText,"Ì,Í,Î,Ï,ì,í,î,ï","I,I,I,I,i,i,i,i")>
<cfset parsedText = replaceList(parsedText,"Ð,ð,Ñ,ñ","Eth,eth,N,n")>
<cfset parsedText = replaceList(parsedText,"Ò,Ó,Ô,Õ,Ö,Ø,ò,ó,ô,õ,ö,ø","O,O,O,O,O,O,o,o,o,o,o,o")>
<cfset parsedText = replaceList(parsedText,"Ù,Ú,Û,Ü,ù,ú,û,ü","U,U,U,U,u,u,u,u")>
<cfset parsedText = replaceList(parsedText,"Ý,ý,ß,ÿ","Y,y,ss,y")>
<cfset parsedText = replaceList(parsedText,"‘,’,“,”","',',"",""")>
<cfset parsedText = replace(parsedText,"™","(TM)","ALL")>
<cfset parsedText = replace(parsedText,"¢","cent","ALL")>
<cfset parsedText = replace(parsedText,"¥","yen","ALL")>
<cfset parsedText = replace(parsedText,"®","registered trademark","ALL")>
<cfset parsedText = replace(parsedText,"±","plus or minus","ALL")>
<cfset parsedText = replace(parsedText,"µ","micro","ALL")>
<cfset parsedText = replace(parsedText,"¼","1/4","ALL")>
<cfset parsedText = replace(parsedText,"½","1/2","ALL")>
<cfset parsedText = replace(parsedText,"¾","3/4","ALL")>
<cfset parsedText = replace(parsedText,"©","copyright","ALL")>
<cfset parsedText = replace(parsedText,"£","pound sterling","ALL")>
<cfset parsedText = replace(parsedText,"&&","&amp;","ALL")>
<cfoutput>#parsedText#</cfoutput>

Friday, March 12, 2010

Break a String at a Specific Character Width

This snippet will break a string into lines that contain no more than the specified character count. It will not chop off a line mid word, instead it will place the line break at the last space before the line break character limit.

This snippet can be useful for sending plain text email where the character count of each line must not exceed a certain length.

<cfset string = "">
<cfset size = "80">
<cfset lineBreak = chr(13) & chr(10)>

<cfset outputString = "">
<cfloop list="#string#" index="word" delimiters=" ,#chr(13)##chr(10)#,#chr(13)#">
<cfif len(trim(listLast(outputString,lineBreak)) & " " & word) gt size>
<cfset outputString = outputString & lineBreak>
</cfif>
<cfset outputString = outputString & word & " ">
</cfloop>
<cfset outputString = replace(outputString," " & lineBreak,lineBreak,"ALL")>

<cfoutput><pre>#trim(outputString)#</pre></cfoutput>

Thursday, March 11, 2010

Superscript Day in Date (eg: 1st, 2nd, 3rd, 4th, etc.)

Ever need to format a date into a readable format with the day followed by the two letter ending sound, eg: 1st, 2nd, 3rd, 4th, etc.

No easy way to do this but here is 'a way':
<cfset thedate=" now()">
<cfset superscriptlist="stndrdthththththththththththththththththstndrdthththththththst">
<cfset superscript=" mid(superScriptList,(day(theDate)*2)-1,2)">
<cfoutput>#dateformat(theDate,'Mmmm d')#<sup>#superScript#</sup>, #dateformat(theDate,'yyyy')#</cfoutput>

Input: now() (in this case {ts '2010-03-11 00:00:00'} )
Output: March 11th, 2010

Uppercase First

Something simple to start with. ColdFusion comes with functions to uppercase and lowercase strings - ucase(string) and lcase(string). There is currently no function to uppercase the first character then lowercase the remaining characters of a string. Here is the solution:
Uppercase the first character of each word in the string:
Snippet: #reReplace('text string',"(^[a-z]|\s+[a-z])","\U\1","ALL")#
Input: 'text string'
Output: 'Text String'

Uppercase the first character of the first word in the string:
Snippet: #reReplace('text string',"(^[a-z])","\U\1","ALL")#
Input: 'text string'
Output: 'Text string'

Welcome to 'ColdFusion Snippets' blog

Hello and welcome to my 'ColdFusion Snippets' blog. Here I'll be sharing all the snippets I use when programming ColdFusion, some of them made by myself, some found while surfing the web.