Easy Flash AS3 Password Validation

Here’s a really easy way to do password validation in Flash with AS3 using a few text fields and button component.

Here’s the code:


//password
var pass:String = "pass123";

//submit label
btn_submit.label = "Submit";

//event listeners
btn_submit.addEventListener(MouseEvent.CLICK, checkPassword);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);

//set focus to input field
password_txt.stage.focus = password_txt;

//check input text field to pass string
function checkPassword(event:MouseEvent):void{
	if(password_txt.text == pass){
		feedback_txt.text = "Correct.";
		stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
	}else{
		feedback_txt.text = "Incorrect.";
	}
}

//enables keyboard enter key
function keyDownListener(e:KeyboardEvent) {
    if (e.keyCode == Keyboard.ENTER){
		checkPassword(null);
	}
}

Download Source

7 Comments »