Andy Jarrett // Code. Develop. Create.

CFLOOP and a little weirdness

I've found and odd thing with CF, it might not to be weird to some of you its just not how i thought it would work.

I am currently passing over three values from a form field, for this we'll call them val1, val2, val3. On the validation side i wanted to check if they are numeric. I've done this by using a simple loop (though i changed to regEx later).
<cfloop list="#form.val1#,#form.val2#,#form.val3#" index="idx">
\n      <cfif NOT isNumeric(idx)>
\n       NOT PASSED
\n       <cfbreak>
\n      </cfif>
\n   </cfloop>

and set the values at:
form.val1 = ""
form.val2 = ""
form.val3 = ""

Obviously this will produce a list that looks like ",,,". Which to CF mean that the list is empty and the CFLOOP will not even execute! Which means that the three values pass as valid. My problem now being that i want the CFLOOP to loop whether the values are NULL or not. To do this i changed the list properties.
<cfloop list=" #form.val1#, #form.val2#, #form.val3#" index="idx">
Note the spaces.
Now if the values are NULL the CFLOOP will still loop using the spaces i've added and give me the verdict i was looking for! So using the original loop(hope you're still with me) i set the values to:

form.val1 = " "
form.val2 = " "
form.val3 = " "
And as thought the list doesn't pass because " " is not numeric (obviously) and i see the word "NOT PASSED". So if you're still with me we now know that CF registers the space as a non numeric string as isNumeric() with return a negative. With this in mind then technically "28 " shouldn't pass either because the third character is a " ", and CF registers this as a non numeric string.

BUT (there's the famous but) it passes it as numeric and all validates????? So how come in this case the string passes. I can only assume it's CF ways of recognising that the string is numeric and you can't have " " in numeric strings. I don't think its a bug or anything i just got caught of guard with it so i thought worth a blog :o)

I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee. Your support keeps this site running and the coffee brewing! ☕️