Friday, 3 December 2010

Actionscript 3 email validation

When you submit form data, you may need to check if the characters in an email address are correct. In this quick tip I will show you how to validate an email address in Actionscript 3. I will be using the isEmailValid() function below which uses a regular expression to determine if the address is valid. The function will return true is the email is valid and false if it’s invalid.

function isEmailValid(email:String):Boolean {
	var pattern:RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
	return email.match(pattern) != null;
}

Here is an example that displays a message if the correct/incorrect email address is entered.

//Adds a mouse click listener to the submit button.
submit.addEventListener(MouseEvent.CLICK, checkEmail);

//This function displays the appropriate message 
//when an email address is email.
function checkEmail(e:MouseEvent):void{
	if(isEmailValid(email_txt.text)){
		valid_txt.text = 'Email address is valid';
	}else{
		valid_txt.text = 'Email address is invalid';
	}
}

//This function will return true if the email address is valid. 
function isEmailValid(email:String):Boolean {
	var pattern:RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
	return email.match(pattern) != null;
}


0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP