Flash AS3 Tutorial: Validate email address in AS3

flash-logo

Are you using AS3 and wondering how you can validate if the user submits valid email address?

You can use textfield in your Flash banner or site.

The code below checks standard email patters of the string entered by user which has “@” sign and the domain ends with “.net” or “.com” etc. which is normally 3 character long.

You can simply use the following function in AS3 to validate user email addresses, and call in your flash project or banner. This is similar ActionScript code that you normally do in JavaScript within your browser, if you don’t have flash content for your form.

Sample code in AS3 to validate email address :

function isValidUserEmail(email:String):Boolean {
var myRegExp:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
var myResult:Object = myRegExp.exec(email);
if (myResult == null) {
return false;
}
return true;
}

// Sample code for Button Listener. submit_btn is the submit button instance
submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);

// Button Click Function. email_txt is instance name of text input.

function ValidateAndSend(event:MouseEvent):void{

if (isValidUserEmail(email_txt.text) == false) {

status_txt.text = “Please enter a VALID email address.”;

} else {

status_txt.text = “Thank you. Your Email is valid.”;
}

}

Click here to learn, what is function?

Hope this code helps. If you have any questions please post your comment below, I’ll get back to you! Also let us know if you find this code helpful!