Avg. Rating 3.3

Problem

You need to validate an email address (ie confirm that a given string is actually a valid email address).

Solution

Use a regular expression to check if the email address entered is correctly formatted and return either true or false.

Detailed explanation

Assuming you want to have some textfield validation for an email input in a pure AS3 project or Flash Project (on those where you don't have access to the flex framework's validation functions).

Normally an email address consists of a few standard patterns, you have the @ sign and the domain ending ".com, .net" normally these are all 3 characters long, but you can easily change the code below to accept more then 3 characters.

public class checkEmail 
{
     public static function check ( s:String ):Boolean
     {
          var regExpPattern : RegExp =
/^[0-9a-zA-Z][-._a-zA-Z0-9]*@([0-9a-zA-Z][-._0-9a-zA-Z]*\.)+[a-zA-Z]{2,4}$/;

if( mail.match(regExpPattern) == null ) { return false; } else { return true; } } }

Then in your own mainclass or frame you just use:

checkEmail(myTextField.text);

For more informations about Regular Expressions, please visit Grant Skinners RegExp Explorer.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes