Treby
Treby

Reputation: 1320

Flex: Text Input that accepts number only

Need a code that only accepts numbers. Upon inputting, the code must check if it is number, if not, it must remove the entered key or not enter it at all

Upvotes: 24

Views: 37929

Answers (8)

zvjerka24
zvjerka24

Reputation: 1792

I'm not sure what exactly you want to do. If you just want to sum those two, use following

{parseInt(txt1.text) + parseInt(txt2.text)}

your example just concatenate those two strings. This one example try to convert text into number and then sum those two values.

Upvotes: 0

Rahul Singhai
Rahul Singhai

Reputation: 1339

I use somthing like

<s:TextInput id="textInput"
    restrict="0-9.\\-"
    change="onChangeNumberTextInput(event, 6)"/>

private function onChangeNumberTextInput(event:TextOperationEvent, precision:uint = 2):void
    {
        var strNumber:String = "";
        if (event.currentTarget is mx.controls.TextInput)
            strNumber = (event.currentTarget as mx.controls.TextInput).text;
        else if (event.currentTarget is spark.components.TextInput)
            strNumber = (event.currentTarget as spark.components.TextInput).text;
        else
            return;

        var ind:int = strNumber.indexOf(".");
        if (ind > -1)
        {
            var decimal:String = strNumber.substring(ind + 1);
            if (decimal.indexOf(".") > -1)
                strNumber = strNumber.substring(0, ind + 1 + decimal.indexOf("."));
            if (decimal.length > precision)
                strNumber = strNumber.substring(0, ind + 1 + precision);
        }

        if (event.currentTarget is mx.controls.TextInput)
            (event.currentTarget as mx.controls.TextInput).text = strNumber;
        else if (event.currentTarget is spark.components.TextInput)
            (event.currentTarget as spark.components.TextInput).text = strNumber;
    }

The change listener function removes everything beyond the number of precision characters from the decimal point, or any second occurrence of ".":

Upvotes: 1

Ryan Watts
Ryan Watts

Reputation: 641

You need to change the property so that the application only request the number keyboard from the application.

try 'SoftKeyboard"number" ; '

Upvotes: 0

sly1024
sly1024

Reputation: 31

There's a control called NumericStepper.

See: http://livedocs.adobe.com/flex/3/html/help.html?content=controls_11.html

If you don't want the up and down arrows there, you can set their skin class to null.

Cheers, Sly

Upvotes: 3

wilko
wilko

Reputation: 21

<?xml version="1.0"?>
<!-- Simple example to demonstrate the TextInput control. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html">

    <mx:Panel title="Dodawanie dwóch liczb :)" height="279" width="238" 
        paddingTop="10" paddingLeft="10">

        <mx:TextInput id="src"
          restrict="0-9"
                maxChars="20" />
        <mx:TextInput id="dest"
          restrict="0-9"
                maxChars="20"/>

        <mx:Button label="dodaj" click= "dodaj();" id="but"/>
        <mx:Label text="Suma" width="59"/>
        <mx:Label text="0" width="160" id="wynik"/>

    </mx:Panel>
    <mx:Script>
     <![CDATA[
      import mx.formatters.NumberBase;
      public function dodaj():Number
      {
       var liczba:Number = Number(src.text) + Number(dest.text);
       wynik.text = liczba.toString();
       return 0;
      }

     ]]>
    </mx:Script>
</mx:Application>

Upvotes: 2

Mihai Nita
Mihai Nita

Reputation: 5787

Look at mx.validators.NumberValidator: http://livedocs.adobe.com/flex/3/langref/mx/validators/NumberValidator.html

Upvotes: 1

Sri
Sri

Reputation: 5845

   <s:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />
   <mx:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />

Upvotes: 13

Gregor Kiddie
Gregor Kiddie

Reputation: 3119

look at the restrict property on the TextInput class. Set it to "0-9"

Upvotes: 31

Related Questions