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

21,521

Solution 1

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 >

Solution 2

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.

Solution 3

<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.

Share:
21,521
Reno
Author by

Reno

Updated on March 27, 2020

Comments

  • Reno
    Reno about 4 years

    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!

  • Peter Boughton
    Peter Boughton over 12 years
    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.
  • Peter Boughton
    Peter Boughton over 12 years
    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.)
  • Peter Boughton
    Peter Boughton over 12 years
    Uh, the OP didn't give any indication of not wanting negative numbers? :/
  • Shawn Holmes
    Shawn Holmes over 12 years
    His tag is coldfusion-8. Your example also shows a cast from a Java null. Not the same as <cfset x = NULL />
  • Peter Boughton
    Peter Boughton over 12 years
    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).
  • Shawn Holmes
    Shawn Holmes over 12 years
    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.
  • Saad A
    Saad A over 8 years
    sorry I didn't mean to down vote, it accidently click. My apologies