views:

3908

answers:

6

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

+6  A: 

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

Gregor Kiddie
how about decimal point. can i include that too?
Treby
Yes, it's just ".0-9" if I recal correctly. Note that they'll be able to add more than one . if you do it this way. If you are restricting them to make a legal number, you'll need some extra AS to handle it.
Gregor Kiddie
+3  A: 
   <s:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />
   <mx:TextInput id="textInput"
                restrict="0-9"
                widthInChars="20"
                maxChars="20" />
Srirangan
A: 

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

Mihai Nita
A: 

thank you gregor, it worked.

but how do i add numbers entered in two text input boxes ?

if i use {txt1.text + txt2.text}, it appends and dosent sum up

sen
-___- Ask in the comments section.
M28
hey ask another question, don't ask here.. its illegal.. ü
Treby
A: 

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.

zvjerka24
+1  A: 
enter code here

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

enter code here
wilko