Justidude
Justidude

Reputation: 917

In ActionScript 3 how do you change the behavior of a text box dynamically

I am trying to change the input text behavior from "Single Line" to "Password" and back based on user input such that when the user types an incorrect password the text field will turn into a Single Line text box with the text "Incorrect Password". Then once they start typing again the text box will once again behave as a Password type text box.

Upvotes: 0

Views: 1423

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Here is something that does what you want, it will display Incorrect Password in the TextField until the correct password is entered.

package 
{
    public class Main extends Sprite 
    {
        private var tf:TextField = new TextField();

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);


            tf.border = true;
            tf.displayAsPassword = true;
            tf.multiline = false;
            tf.height = 20;
            tf.type = TextFieldType.INPUT;
            tf.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            tf.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);

            addChild(tf);

        }

        private function onFocusIn(e:FocusEvent):void 
        {
            tf.text = "";
            tf.displayAsPassword = true;
        }

        private function onKeyDown(e:KeyboardEvent):void 
        {
            if (e.keyCode == Keyboard.ENTER)
            {
                if (tf.text == "pass")
                {
                    trace("logged in");
                }
                else
                {
                    tf.displayAsPassword = false;
                    tf.text = "Incorrect Password";
                    stage.focus = stage;
                }
            }
        }

    }

}

Upvotes: 2

Related Questions