You need to test a string to see if it is a valid numeric value.

The isNumeric() function is used to directly test a variable to see if it is numeric.

 

<cfset testVar = "foo">

<cfoutput>#isNumeric(testVar)#</cfoutput>

NO

A Boolean response (True or False) is returned.

The val() function on the other hand, will go beyond an all or nothing check. It will attempt to parse out a number from the beginning of a string, and return this number if it exists:

<cfset testVar = "100foo">
<cfoutput>#val(testVar)#</cfoutput>
100

If the beginning of the string contains a number, that number is returned. If not, a 0 is returned.

This question was written by Jeremy Petersen
It was last updated on January 12, 2006.

Categories

Numbers

Comments

comments powered by Disqus