21.06.2009

AS3 Email Validation

Here is one of the easiest ways to validate an email in AS3.

When your designing a flash application that has forms…it is essential that you validate your emails one way or another. If you choose to do all your validation on the server-side and return the error value directly into flash…then thats one way to do it.

Personally, i would always put my form through a number of simple checks before sending the data to the server.
I generally use PHP to handle form data, so from now on i will refer to the server-side as PHP.

Here is a quick and easy way to validate an email:

Lets assume your using a ’submit’ button to submit your form data to PHP. The following code would be familiar:

buttonSubmit.buttonMode = true;
buttonSubmit.useHandCursor = true;
buttonSubmit.addEventListener(MouseEvent.CLICK, submitForm);
 
function submitForm(e:MouseEvent):void
{
 
}

Okay, so whats the code you need to actually validate the structure of an email? well its very simple…1 line of regular expression.

function isValidEmail(email:String):Boolean {
    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    return emailExpression.test(email);
}

As you can see, this function takes an email address ’string’ and checks that the structure of the string is recognised as an email address. Whether or not the email address exists..is a server-side matter.

So how do you use it? it’s very easy, and here is the code:

buttonSubmit.buttonMode = true;
buttonSubmit.useHandCursor = true;
buttonSubmit.addEventListener(MouseEvent.CLICK, submitForm);
 
function submitForm(e:MouseEvent):void
{
    if( !isValidEmail(yourTextFieldName.text) )
    {
        //email address is not valid
    }
    else
    {
        //email address is valid
        //form submit code goes here
        //
        //
    }
}
 
function isValidEmail(email:String):Boolean {
    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    return emailExpression.test(email);
}

And thats it…too easy. All you would need to add is a text box which display some sort of error message, telling the use the email is not valid.

I have included the source file which includes an error prompt. Enjoy!

AS3-Email-Validate.fla

4 comments for this post

Roy

July 2nd, 2009, 7:48 pm

This script returned
«something@email…com» as valid ???
«123456@email.com» as valid ???

lukegill

July 4th, 2009, 12:34 pm

The reason that 12345@email.com is shown as valid..is because you can actually have 123456 as your email name. It obviously depends on your mail server. Secondly, the reason something@email….com s shown as valid is because there is no way to determine how many ‘.’ are used after an ‘@’ sign. Somebody may have an email address ’something@something.mail.ac.uk’ (3 ‘.’s) For this reason, the only way to 100% validate the email would be to get php etc to run a DNS check on the provided email address and return the result.
Since actionscript is client-side we are limited to basic validation without the use of a server-side language.

Hope this answers your questions.

ale

July 16th, 2009, 11:05 am

Many thanks!

Adam

October 29th, 2009, 4:31 pm

But, my email address, a@…, does not work because I have only one letter to the left of the @ symbol and it doesn’t accommodate people with underscores in their addresses too. I used this instead. It handles .COMs and .CO.UKs, too:

/([a-z0-9._-]+?)@([a-z0-9.-]+)\.([a-z]{2,4})/;

Cheers,
Adam

leave a comment