Thursday, January 29, 2015

How to find spaces in a form field in Coldfusion

This is pretty straight forward

<cfset strTest = "#form.variable#">
<cfset strTarget = " ">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>

This will count the number of spaces in the variable.

If the intCount = 0, you are good to go.

You can use the code to test for any character.

Enjoy!