Search code examples
coldfusioncfmlduck-typingcoldfusion-2016

Strings with newlines should not treated as numbers


I don't want strings with newlines treated as numbers.

This should be considered a string

<cfscript>
  notes = "3
    ";
</cfscript>

The newline is preserved

 <cfoutput>
 <pre>|#notes#|</pre>    

 Number: #isNumeric(notes)# <!--- returns YES --->

 <pre>|#replacelist(notes, chr(10) , "\n") #|</pre>    

 Number: #isNumeric(notes)# <!--- returns YES
 </cfoutput>

Example on cffiddle

enter image description here


Solution

  • I'm always hesitant to write an answer when I write from my head and have no written proof of what I'm saying. ColdFusion has various functions to validate data. There's isValid and isInteger and so forth. I'm not using these functions much because they validate values very liberally in some cases. For instance the Dollar sign will be accepted in some cases when there should be an integer value. I Therefore use regular expressions to check whether a value is numeric. reFind( "^\d+$", ... ) only allows digits. This expression allows an optional minus in front: reFind( "^-?\d+$", ... )