Email Validator for Flash

Okay so far I have not been very consistent on this blogging thing. I have been really busy lately. I plan on posting more often. I have been working a lot in Flex (Flash Builder 4 Beta). The other day I was building a form and took a look at the EmailValidator class in the Flex Framework. The class is pretty sweet and appears to be pretty robust. Sometimes that is exactly what you want, however sometimes you just want a super simple, easy-to-use email validation method. Because of that need I wrote a really simple email validation class. It is really simple and basically just checks for an ‘@’ symbol and a ‘.’ character, but it is really easy to implement. It’s not like the other email validators actually validate the email anyway. They may be more complex, but it’s not like they actual validate it by sending an email. So anyway I have included the source code to this class and if you are looking for an easy way to validate an email feel free to use it and adapt it as your heart desires. It is written in ActionScript 3.0, but it would be really easy to convert it to AS 2.0 if for some reason you were programing in that. Here is what the code looks like:

package
{
    public class EmailValidator
    {
        public static function validateEmail(emailAddress:String):Boolean
        {
            var testsPassed:uint = 0;
            if(emailAddress.indexOf('@') > 0)
                testsPassed++;
            if(emailAddress.lastIndexOf('.') > emailAddress.indexOf('@'))
                testsPassed++;
            if(testsPassed == 2)
                return true; else return false;
        }
    }
}

Here is a link to the source files: Email Validator The .fla file was saved in Flash CS4 so to open it you would need that version or later. I have written a bunch of other cool classes in Flash that I feel are pretty handy. I will post some others soon. If you are just starting to do Object Oriented Programming in Flash or have been doing it for a while and don’t have a collection of custom classes yet, I would highly recommend starting one. There are a lot of good classes out there that are free. I have benefited from many such classes and tutorials and hope to give back on this site and blog. Let me know how you like them.

Leave a Reply

Your email address will not be published. Required fields are marked *