View Full Version : How to use TextFields as "Number Fields"?
alex298
June 8th, 2008, 07:30 AM
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
dthought
June 8th, 2008, 12:32 PM
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.
alex298
June 8th, 2008, 11:48 PM
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
littlespex
June 9th, 2008, 12:02 AM
Add a listener to myTextField for the Keyboard.KEY_DOWN event that checks that event.charCode is between 48 and 57:
import flash.events.KeyboardEvent;
myTextField.addEventListener(KeyboardEvent.KEY_DOW N, 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");
}
}
amarghosh
June 9th, 2008, 05:18 AM
You may also wish to look at using something like myTextField.restrict = "0-9"; to ensure only numbers are entered.
textField.restrict .... i didn't know that.. thanks :)
alex298
June 10th, 2008, 01:33 AM
Hi all,
Thanks for your help. It works perfectly:)
Best regards
Alex
manuraj.dhanda
June 10th, 2008, 12:14 PM
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.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.