Checking Or Testing If a Value / Variable Is Integer (number) In Coldfusion
For all of you beginner coldfusion programmer there who are wondering on how you can check or test if a value or your variable is a numeric or not.This is of course is important because we do not want a wrong data type to use in our where clause with our cfquery right?
This is actually very easy, just use:
#isnumeric(value)#
With isnumeric function you can test if a value is an integer or not, here is a basic example.
cfset myvariable="12345"
cfif #isnumeric(myvariable)# eq true
cfset result="variable is numeric"
cfelse
cfset result="variable is not numeric"
/cfif
#result#
Of course we would put a greater than and less than sign at every start and end each line, but I know you can understand the purpose of this code.
Also, I tried doing
cfif #isnumeric(myvariable)# eq "true"
and the result is still the same.
Well, that's all for now, hope this helped you.
Happy programming and good luck to you project!
Wrong. isNumeric doesn't check to see if it's an integer; it checks to see if it's a number. So if you send in 12.5783, it will return true.
ReplyDeleteThis is admin and thank you for the correction.
ReplyDeleteit's more of a slip of the tongue I guess.
& lt; gives (without the space) will give you the left angle bracket.
ReplyDelete<cfloop list="2.123,a,4" index="myVal">
<cfif !isNumeric(myVal)> ...error code for not numeric
<cfelseif myVal neq int(myVal)> ...error code for not integer
<cfelse>is integer
</cfif>
</cfloop>