# How to use TextFields as "Number Fields"?

**URL:** <https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638>\
**Category:** flash\
**Created:** [June 8, 2008, 10:30am UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638 "2008-06-08T10:30:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [June 8, 2008, 10:30am UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/1 "2008-06-08T10:30:16Z")

</div>

Hello,

I have two TextFields on the stage. Actually I would like to use the two TextFields as Numbers. Therefore I can do some calculation of the two TextFields. Is it possible to do that? If yes, could you please provide some guidelines?

Thanks and best regards

Alex

---

<div class="post-metadata">

**Author:** ![dthought](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dthought/32/3573_2.png) [@dthought](https://forum.kirupa.com/u/dthought)\
**Post date:** [June 8, 2008, 3:32pm UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/2 "2008-06-08T15:32:52Z")

</div>

One way would be to coerce the String value of the text into a Number, like so:

firstNumber = Number(myTextField.text);

You may also wish to look at using something like myTextField.restrict = “0-9”; to ensure only numbers are entered.

---

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [June 9, 2008, 2:48am UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/3 "2008-06-09T02:48:16Z")

</div>

Hi dthought,

Thanks for your help. This is exactly what I need, and with one more extra bonus - myTextField.restrict = “0-9”;

I have one more question:

The stage has another dynamic TextField (output\_txt). I wish to alert users that the myTextField is restrict to numbers only. When the users not enter numbers in the myTextField, a message (e.g. Only numbers are allowed) will appear in the output\_txt TextField. How can I do that?

Thanks and best regards

Alex

---

<div class="post-metadata">

**Author:** ![littlespex](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@littlespex](https://forum.kirupa.com/u/littlespex)\
**Post date:** [June 9, 2008, 3:02am UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/4 "2008-06-09T03:02:36Z")

</div>

Add a listener to myTextField for the Keyboard.KEY\_DOWN event that checks that event.charCode is between 48 and 57:

```auto

import flash.events.KeyboardEvent;

myTextField.addEventListener(KeyboardEvent.KEY_DOWN, numberCheck);

function numberCheck(event:KeyboardEvent):void
{
    if (event.charCode >= 48 && event.charCode <= 57)
    {
        trace("User input is a number");
    }
    else
    {
        trace("User input is not a number");
    }
}

```

---

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [June 10, 2008, 4:33am UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/5 "2008-06-10T04:33:49Z")

</div>

Hi all,

Thanks for your help. It works perfectly:)

Best regards

Alex

---

<div class="post-metadata">

**Author:** ![manuraj\_dhanda](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@manuraj\_dhanda](https://forum.kirupa.com/u/manuraj_dhanda)\
**Post date:** [June 10, 2008, 3:14pm UTC](https://forum.kirupa.com/t/how-to-use-textfields-as-number-fields/262638/6 "2008-06-10T15:14:06Z")

</div>

You can make use of validators in ActionScript 3.

\<mx:NumberValidator source="{txtRent}" property=“text” required="true"  
integerError=“Enter Integer value” minValue=“50” domain=“int”/\>

You should use source as the textfield name. minValue is the minimum value that this textField will allow you. Same way you can also define maxValue.

Have fun!!  
Manu.
