user3010564 user3010564 - 7 months ago 81
ActionScript Question

Automatically detect the value of an input text field in AS3

I have an input text field, where the user inputs a number, the user clicks on a "validate" button and i check his answer, if it equals "2" the answer is right, else the answer is wrong.

I used two icones, the "greenSign" for the right answer, and the "redSign" for the wrong one, those two icones are initially invisible, if the user inputs a number, one of these signs show up according to his answer.

Screen img: https://image.ibb.co/b9Ems6/aze.png.

the code:

greenSign.visible=false;
redSign.visible=false;

txt1.type = TextFieldType.INPUT;

validationButton.addEventListener(MouseEvent.CLICK, textInputCapture);
function textInputCapture(e:MouseEvent){
if(txt1.text=="2")
{redSign.visible=false; greenSign.visible=true;}
else
{redSign.visible=true; greenSign.visible=false;}
}


I want to verify what the user entered in the input text field without using the "validate" button. if the user inputs "2", the "greenSign" shows up automatically, and so on. any idea?

Answer Source

TextFields also dispatch Events, just like your Button. Look at this list.

I suggest the KeyUp Event, which will fire every time on the Up-Phase when somebody presses a Key on their Keyboard.

txt1.addEventListener(KeyboardEvent.KEY_UP, textInputCapture);

function textInputCapture(e:KeyboardEvent):void{...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download