4 Replies Latest reply on Nov 26, 2010 1:57 PM by SpiritOfTheWinds

    isNaN not working properly?

    SpiritOfTheWinds Level 1

      Hello,

       

      I am back (again) with another problem.

       

      I've noticed that by using isNaN(number), it doesn't always return true when needed. In fact, by using:

       

      var bool:Boolean = isNaN(parseFloat(numberStr));
      trace(bool);

       

      It will return true with:

      -sdfda

      $%&$

      asdf

       

      BUT, false (when it should be true) with:

       

      230asdfs

      230sdfasd.50

       

      It seems like the letters between the numbers aren't even considered and it returns false when it should in fact return true... If it starts with a letter, it returns true, otherwise, with a number, it's false...

       

      It there a solution to this? I need to check whether or not there are any non-numeric (except "dot") in the parameter "numberStr" (a String).

       

      Thanks!

        • 1. Re: isNaN not working properly?
          Ned Murphy Adobe Community Professional & MVP

          I don't see the problem.  If something is Not a Number, then isNaN will produce a true output, which you say is what it does.

           

          But when you use parseFloat, per the help dcumentation, if the string begins with a number, then is parses up to the first character that is not part of a number and rejects the rest.  So both parseFloat("230asdfs") and parseFloat("230sdfasd.50") will produce a value of 230, which is a number, so isNaN will output false.

          • 2. Re: isNaN not working properly?
            SpiritOfTheWinds Level 1

            Oh, thanks for the information, I didn't know about it... In fact, I never thought about it...

             

            I thought I couldn't use a string in isNaN cause of the docs that says "isNaN(num:Number)"... but well, it works. I've simply put my string in it, without any parse.

             

            Thanks for the reminder.

            • 3. Re: isNaN not working properly?
              Ned Murphy Adobe Community Professional & MVP

              You're welcome.  As long as the string is a number, the isNan can determine if it is or not, it sort of does its own little parsing act, though it evaluates the entire string. This aspect of its functionality is not well documented/demonstrated.

               

              I have CS3, so I don't know what the newer versions offer, but in CS3, isNan is specified as ... isNaN(expression:Object)

              • 4. Re: isNaN not working properly?
                SpiritOfTheWinds Level 1

                Seems like the doc I used was misleading... The one from adobe help is written isNaN( expression )...

                 

                Anyway, thanks, it was still a big help.