3 Replies Latest reply on Dec 14, 2010 11:27 PM by rzt_mtc

    Text Field Number Validation

    rzt_mtc

      Okay, I'm trying to figure out Flash CS5 on my own, and this should be easy, but... All I want to do is validate that the text entered in a text field is a number when a button is clicked. It can be a positive, negative, or decimal, but no letters or symbols.

       

      Is there a simple way to do this that I'm missing? Any help would be appreciated!!!

        • 1. Re: Text Field Number Validation
          Ned Murphy Adobe Community Professional & MVP

          You can use the isNaN() function to determine if the text evaluates to a numeric value.  You can also use the restrict property of a textfield to limit the characters that are allowed to be entered in to it.

          • 2. Re: Text Field Number Validation
            rzt_mtc Level 1

            Thanks, Ned. I tried using the isNaN function, but I got the following error:

             

            Scene 1, Layer 'Layer 1', Frame 1, Line 5    1067: Implicit coercion of a value of type String to an unrelated type Number.

             

            If I include parseInt() it works, but then tells me that 1,000 is a number, despite the ",".

             

            Here's the code:

             

            import flash.events.MouseEvent;
            btnCalc.addEventListener(MouseEvent.CLICK, doCalc);

             

            function doCalc(e:MouseEvent):void {
                if (isNaN(inputPV.text)) { // this works, but...
                    trace(parseInt(inputPV.text) + " is not a number.");
                } else {
                    trace(parseInt(inputPV.text) + " is a number.");
                }
            }

             

            Any help is appreciated!!

            • 3. Re: Text Field Number Validation
              rzt_mtc Level 1

              Oh wait, it works if I use a Number() function, too. In other words,

               

                  if (isNaN(Number(inputPV.text))) {
                      trace(Number(inputPV.text) + " is not a number.");
                  } else {
                      trace(Number(inputPV.text) + " is a number.");
                  }

               

              Thanks again for pointing me in the right direction, Ned.