I'd write (I'm assuming you want integral quantities too).... 

<cfset orderQty = int(val(form.qty))>
<cfif orderQty lte 0>
        <cfset formError = "Quantity value must be greater than zero.">
</cfif>

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Brett
Payne-Rhodes
Sent: Monday, 5 September 2005 7:45 p.m.
To: CFAussie Mailing List
Subject: [cfaussie] Using val() rather than isNumeric()

I just thought I would share this with everyone. Another lesson in RTFM!

I'm a long time user of the 'val()' fumction but have just become aware
of a serious mistake in my use of the function... It doesn't do the same
thing as isNumeric(), which is how I've been using it. The problem has
only arrisen because I've been using val() to check that I can get a
numeric value from a string but then *not* using it in the actual
assignment.

For example (very simplified):

<cfset form.qty = "33kg">

<cfif val(form.qty) gt 0>
    <cfset orderQty = form.qty>
<cfselse>
    <cfset formError = "Quantity value must be greater than zero.">
</cfif>

Results in an 'orderQty' value of "33kg", when the intent is for the
error message to be generated. The problem is that when I later go to
use the orderQty in a calculation the whole thing blows up! But didn't
it take a long time to find when I was assuming that <cfif val()> was
protecting my code!

I should have been doing:

<cfif val(form.qty) gt 0>
    <cfset orderQty = val(form.qty)>
<cfselse>
    <cfset formError = "Quantity value must be greater than zero.">
</cfif>

Which would result in an orderQty of 33, which in the circumstances
would be acceptable.

Or:

<cfif isNumeric(form.qty)>
    <cfset orderQty = form.qty>
<cfselse>
    <cfset formError = "Quantity value must be greater than zero.">
</cfif>

Which would generate the error message.

And no I don't know why I'm using val() instead of isNumeric(), perhaps
I'm just lazy and couldn't be bothered typing 'isNumeric()' all the time
- 'val()' is so much easier...

Brett
B)

-- 
Brett Payne-Rhodes
Eaglehawk Computing
t: +61 (0)8 9371-0471
f: +61 (0)8 9371-0470
m: +61 (0)414 371 047
e: [EMAIL PROTECTED]
w: www.ehc.net.au
w: www.yoursite.net.au

---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to
[EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/




---
You are currently subscribed to cfaussie as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]
Aussie Macromedia Developers: http://lists.daemon.com.au/

Reply via email to