Determining if a string is not null/blank and is a number and not 0?
Asked Answered
H

4

6

I normally don't work in ColdFusion but there's a FTP process at work I have to create a report for with the only option right now being a ColdFusion 8 server. This FTP feed has a few issues (trash too).

So, I make the query and then I need to convert some of the string values during the output to do some math. Before that:

How do I tell if a field in the output loop: is not blank or null, is string that can be converted into a valid number, and is not 0?

Is there a simple way of doing this w/o a lot of if statements?

Thanks!

Hurds answered 13/10, 2011 at 17:13 Comment(0)
E
10

So you want to make sure that the variable is numeric but not zero?

Then you want this:

<cfif IsNumeric(MyVar) AND MyVar NEQ 0 >
Eisk answered 13/10, 2011 at 17:26 Comment(0)
P
5

Determining if a string is not null/blank and is a number and not 0?

Here's the code I would use in this case.

<cfif isDefined(stringVar) and len((trim(stringVar))) and isNumeric(stringVar)>
    do stuff here
</cfif>

isDefined returns a true if the variable exists. If you know the scope of the variable, i.e., its in the form or url scope for instance, you can use structkeyExists(form,"stringVar"). I would recommend using this approach if you know the scope of the variable.

Len(trim(stringVar)) is the second check. First off it trims any leading or trailing empty spaces from the string - this makes sure that any empty variables are not passed along. Then if something is there it will return the length of the string. If its empty len will return a 0.

isNumeric(stringVar) returns a true if the variable is a number and false otherwise.

Peerage answered 24/10, 2011 at 20:13 Comment(1)
sorry I didn't mean to down vote, it accidently click. My apologiesWestbrook
M
1
<cfif Len(field) and Val(field)>

Len() will verify the field has length (not blank--there are no NULLs in CF) and Val() will automatically convert the first character in the string into into a number--or return 0 if it cannot.

Take note of Peter's comment below; although this is the least verbose answer, Val() may fail in certain edge conditions below, ie. The field is a string but starts with a number, incorrectly converting it to a number, and evaluating to TRUE.

Mile answered 13/10, 2011 at 17:20 Comment(5)
Afraid I have to disagree twice - CF kinda has nulls (and they're a bit quirky) and the string "6a" is not a number, but val('6a') returns 6 and thus passes, which the OP probably doesn't want.Eisk
Oh, just noticed the question relates to CF8, so the blog post linked above on nulls doesn't apply. (Still some potentially useful info in there though.)Eisk
His tag is coldfusion-8. Your example also shows a cast from a Java null. Not the same as <cfset x = NULL />Mile
Well the tag is coldfusion-8 because I added it - right before I came back added the comment above saying the blog post isn't relevant (couldn't edit the original message). :P And I did say kinda because it's not a real null. It might be worth being aware there are ways to get real nulls with both OpenBD and Railo (though I've never been in a scenario where I've needed one).Eisk
I stand corrected on the coldfusion-8 tag. There are certainly "ways" to get NULL into cf, but I would always be certain to clarify that it is not a native type in CF.Mile
L
0
<cfif isNumeric(myfield) and myfield gt 0>
Lather answered 13/10, 2011 at 17:53 Comment(1)
Uh, the OP didn't give any indication of not wanting negative numbers? :/Eisk

© 2022 - 2024 — McMap. All rights reserved.