Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/9140695 to your computer and use it in GitHub Desktop.
Save JamoCA/9140695 to your computer and use it in GitHub Desktop.
What is an integer? Validity, identification & parsed values should be consistent across all ColdFusion functions & tags. A "non-breaking space" chr(160) has been added to tests as I occassionally seen this character imported via CFSpreadsheet.
<!--- 2/21/2014 ColdFusion Number Test
What is an integer? Validity, identification & parsed values should be consistent across all ColdFusion functions & tags. A "non-breaking space" chr(160) has been added to tests as I occassionally seen this character imported via CFSpreadsheet. --->
<cfset isRailo = isDefined("Server.Railo")>
<cfset nbsp = chr(160)>
<cfset BadValues = ["1 5", " 5", "5 ", "3.14", ".99", "2.00", "2147483648", "005", "1+1", "5-1", "2/1", "3*2", "5#nbsp#", "#nbsp#5", "1#nbsp#5", "6,0", "0,6", "1,000", "$1,000", "$,1,2,$,2352345,$", "1,2,3,4"]>
<cfset c = 0>
<cfset TestQuery = QueryNew("i","integer")>
<cfset queryaddRow(TestQuery)>
<style type="text/css">
.fail {color:#f00;}
</style>
<cfscript>
function failMsg(){
var t = "No";
if(arraylen(Arguments) GTE 1) {
t = Arguments[1];
}
return "<span class=""fail"">#t#</span>";
}
</cfscript>
<cfoutput>
<cfloop array="#BadValues#" index="ThisString">
<cfset c=c+1>
<fieldset><legend><b>Test ###c# of #ArrayLen(BadValues)#: "#ThisString#"<cfif find(nbsp, thisString)> (w/non-breaking space chr(160))</cfif></b></legend>
val() = "#val(ThisString)#" <cfif val(ThisString) IS ThisString>Yes?<CFELSE>#failMsg()#</cfif><br>
evaluate() = <cftry>#YesNoFormat(evaluate(ThisString))#<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
IsValid("integer", thisString) = <CFIF IsValid("integer", thisString)>Yes<CFELSE>#FailMsg()#</CFIF><br>
IsValid("float", thisString) = <CFIF isValid("float", thisString)>Yes<CFELSE>#FailMsg()#</CFIF><br>
IsValid("numeric", thisString) = <CFIF isValid("numeric", thisString)>Yes<CFELSE>#FailMsg()#</CFIF><br>
cfparam(int)=<cftry><cfparam name="testInt#c#" type="integer" default="#ThisString#">Yes<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
QofQ CF_SQL_INTEGER = <cftry><cfset querysetcell(TestQuery, "i", ThisString, 1)><cfquery name="test" dbtype="query">select * from TestQuery where i = <cfqueryparam value="#thisString#" cfsqltype="CF_SQL_INTEGER"></cfquery><CFIF test.RecordCount>Yes<cfelse>#failMsg()#</CFIF><cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
Compare Javacast Equality = <cftry>#YesNoFormat(Compare(javacast("int", thisString), thisString))#<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
CompareTo Javacast Equality = <cfif isRailo>not supported<cfelse><cftry>#YesNoFormat(thisString.CompareTo( javacast("int", thisString) ))#<cfcatch>#failMsg()#</cfcatch></cftry></cfif><br>
MOD = <cftry><cfset temp = 1 MOD thisString>Yes<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
"#ThisString#"+1 = <cftry><cfset temp = thisString + 1>Yes? "#temp#"<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry>
<hr>
IsNumeric(ThisString) = #YesNoFormat(IsNumeric(ThisString))#<br>
cfparam(numeric)=<cftry><cfparam name="testNum#c#" type="numeric" default="#ThisString#">Yes<cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry><br>
QofQ CF_SQL_NUMERIC = <cftry><cfset querysetcell(TestQuery, "i", ThisString, 1)><cfquery name="test" dbtype="query">select * from TestQuery where i = <cfqueryparam value="#thisString#" cfsqltype="CF_SQL_NUMERIC"></cfquery><CFIF test.RecordCount>Yes<cfelse>#failMsg()#</CFIF><cfcatch>#failMsg(cfcatch.Message)#</cfcatch></cftry>
</fieldset>
</cfloop>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment