Announcement

Collapse
No announcement yet.

Have I forgotten how to test for numeric data?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Have I forgotten how to test for numeric data?

    In the following code, g.onhand could be blank or a number. I'm running this on the latest Miva version.

    g.OnHand: (&mvte:global:onhand;) <br />
    <mvt:assign name="g.isnumber" value="isdigit(g.onhand)" />
    g.IsNumber: (&mvte:global:isnumber;)

    <mvt:if expr="g.onhand EQ NULL">
    ...
    <mvt:else>
    ...
    </mvt:if>

    If g.onhand is blank, g.isnumber is 1, and my if statement evaluates as true.
    If g.onhand is 0, g.isnumber is 1, and my if statement evaluates as true.
    If g.onhand is greater than 0, g.isnumber is 1, and my if statement evaluates as false.

    Is 0 considered NULL? I thought blank is NULL and 0 is something real. I've even tested this with <mvt:if expr="g.onhand EQ ''"> in place of NULL.
    Also, what's going on with the isdigit() function returning 1 if the variable is blank?

    #2
    0, 1, 2, 3...are all digits.

    IF the data field you are testing is "Text" than empty is empty or null. However, If the data field is a numeric, blank IS equal to 0.

    What are you actually doing though. Why test for the character type? Assuming we are talking about a numeric data field, just test directly

    <mvt:if expr="g.onHand GE 1">
    do this
    <mvt:else>
    do that
    </mvt:if>
    Bruce Golub
    Phosphor Media - "Your Success is our Business"

    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
    phosphormedia.com

    Comment


      #3
      Use len() to test for existence of a value first. isdigit() should be used on an actual string (as all variables are strings).

      Don't use 'EQ NULL', it will never evaluate true AFAIK.

      In MivaScript, testing g.variable returns false for both an empty string and the value 0.
      Gordon Currie
      Phosphor Media - "Your Success is our Business"

      Improve Your Customer Service | Get MORE Customers | Edit Any Document Easily | Free Modules | Follow Us on Facebook
      phosphormedia.com

      Comment


        #4
        The whole purpose is to display different text depending on the existence of a number or not.

        In the documentation I have, isdigit() is a string function that should only return TRUE if all of the characters are numbers 0-9 and FALSE for anything else. Seems like a bug or my documentation is out of date for that function.

        I haven't been writing enough code lately, so I completely forgot that I used to use len(). Thanks for that!

        Comment


          #5
          Originally posted by TFo View Post
          The whole purpose is to display different text depending on the existence of a number or not.

          In the documentation I have, isdigit() is a string function that should only return TRUE if all of the characters are numbers 0-9 and FALSE for anything else. Seems like a bug or my documentation is out of date for that function.

          I haven't been writing enough code lately, so I completely forgot that I used to use len(). Thanks for that!

          Unfortunately, isdigit returns true on a blank string / empty variable. You could do something like this:
          Code:
          <mvt:if expr="NOT ISNULL g.somevar AND isdigit( g.somevar )">
          I am a digit
          <mvt:else>
          I am not a digit
          </mft:if>
          David Carver
          Miva, Inc. | Software Developer

          Comment

          Working...
          X