ayu
ayu

Reputation: 23

How to disable button when textfield is empty

I make a textfield named 'nama' and button 'ayo'. I want to make the button disable when the textfield is empty. But when I try this code my button not disable and still working. I want the button disable before the textfield filling.

stop();
menumulaikuis();
var namasiswa:String;
var nama:TextField = new TextField();
namasiswa = nama.text;

nama.addEventListener(Event.CHANGE,handler);
function handler(event:Event){
    if (nama.text) {
        ayo.enabled = true;
        ayo.visible = true;
    } else {
        ayo.enabled = false;
        ayo.visible = false;
    }
}

Upvotes: 0

Views: 270

Answers (2)

Naomi Dennis
Naomi Dennis

Reputation: 26

If the Text Field isn't for user input, you cannot use the Event.CHANGE listener.

I suggest changing Event.CHANGE to Event.ENTER_FRAME; that should fix your problem.

Upvotes: 0

akmozo
akmozo

Reputation: 9839

You have some little problems in your code :

  • You should add your text filed to the stage using addChild() :
    var nama:TextField = new TextField();
    addChild(nama);
  • If your text field is for user's input, so its type should be input :
    nama.type = 'input';
  • To verify if a text field's text is empty, you can simply do :
    if(nama.text == ''){ /* ... */ }

So your text field's change handler can be like this :

function changeHandler(event:Event): void 
{
    if (nama.text != '') {
        ayo.enabled = true;
        ayo.visible = true;
    } else {
        ayo.enabled = false;
        ayo.visible = false;
    }
}

Hope that can help.

Upvotes: 1

Related Questions