How to Validate an Email Address (ActionScript 1.0 or 2.0)

ActionScript 2.0

When it comes to programming, I generally like to work out my own solutions to a problem, just because it feels so good to finally nail a challenge.  Often enough, of course, someone else beats me to the punch.  Those are the times I study the code until I really understand it — which may require a number of readings! — and then smile in admiration of the author.  I’ve been a fan of Ralf Bokelberg’s programming for years and wasn’t surprised at all to find that he’s written an elegant approach to validating email addresses in ActionScript (pre-AS3; that is, without the benefit of regular expressions). 

His approach uses the String class, which is straightforward enough.  One of its methods, String.indexOf(), allows you to check if a given string contains a certain character (or characters).  For example, you could check a doubted address by looking to see if it contains the @ symbol (clearly a requirement in email addresses):

function isValidEmail(address:String):Boolean {
  if (address.indexOf("@") < 0) return false;
    return true;
}
trace(isValidEmail("user@domain.com"));

If the string contains the passed-in character, the method returns that character’s position in the string.  In the above example, the @’s position is 4.  Because 4 is not less than zero, return false is skipped and the function returns true.  If the string does not contain the character in question, the method returns -1.

Of course, the above example is an extremely weak test — practically useless.  Sure, a missing @ makes an invalid address, but what if the given string has two such symbols?  What if the @ appears after the .com?  Those would be invalid addresses, and the above “catcher” would let those slip right through its fingers.

An answer, short and sweet

Ralf’s solution uses String.indexOf() and others, and he uses the methods in very interesting ways.  His solution is here …

http://www.bokelberg.de/actionscript/checkEmail.html

… and though it doesn’t use AS2’s syntax (num:Number, str:String, etc.), the script works just fine in ActionScript 2.0.

To use it in your own project, either copy/paste it into your timeline or save it as its own file and use the #include directive to pull it into your own code:

#include "bokelberg_validate-email.as"

Then pass in your string and check if the return value is 1.  Anything else means the address is invalid (poorly formed).  Before sending user input to a form mailer, for example, you might check the email text field’s content first …

if (checkMail(inputFieldEmail.text) == 1) {
    // LoadVars instructions, perhaps
  } else {
    // Email no good!  Alert the user, or clear the text field …
    inputFieldEmail.text = "";
 }

How it works

I usually include a “How it works” section in the “How to” posts, but in this case the explanation would take quite a few pages — and in a way, I’d feel a bit odd speaking authoritatively about someone else’s code (at least, about code that inspires and humbles me).

Note Ralph’s use of functions within a function and his compact, efficient use of String.indexOf() and String.charAt() as suppliers of true/false values to the if statements of the interior functions.  I love it.

As an interesting point, the very first couple lines (“implementation of the regular expression,” aka regex) shows a regex pattern that, in a practical sense, performs the same routine on a given string.  All those lines, nested functions and all, can be collapsed into a single, dense line of code that makes perfect sense to a regular expressions engine.  ActionScript 3.0 supports regex.  That pleases me to no end.  :)

Leave a Reply