Sven Dens

deserialize me

Email and cell phone validation in AS2 and AS3

with 6 comments

I just wrote 2 simple classes to validate email addresses and Belgian cell phone numbers in AS2 and AS3.
Might come in handy for you, they’re available for download here.

I included FLA’s for Flash8 and FlashCS3 for AS2 and AS3.
They show 2 TextInput components, one for an email address and one for a cell phone number.
The cell phone validation includes a “strip” function. Since people tend to enter their phone numbers in different ways, dots, blanks and slashes are removed from the input before validating the number.

AS2 Usage example:

import be.boulevart.as2.validation.Email;
import be.boulevart.as2.validation.GSM;

class be.boulevart.as2.playfield.Validation extends MovieClip
{
    // Elements on Stage
    private var check_btn:Button;
    private var email_ti, phone_ti:TextInput;
    private var emailOk:Boolean;
    private var gsmOk:Boolean;

    public function Validation() {}

    private function onLoad():Void
    {
        check_btn.addEventListener("click", Delegate.create(this, doValidation));
        email_ti.tabIndex   = 1;
        phone_ti.tabIndex   = 2;
        check_btn.tabIndex  = 3;
    }

    private function doValidation():Void
    {
        emailOk = Email.validate(email_ti.text);
        gsmOk = GSM.validate(phone_ti.text);

        if(!emailOk)
        {
            Alert.show("Invalid email address!", "Error", Alert.OK, this);
        } else {
            Alert.show("Email address is valid!", "You rock dude", Alert.OK, this);
        }

        if (!gsmOk)
        {
            Alert.show("Invalid cell phone number!", "Error", Alert.OK, this);
        }
        else
        {
            Alert.show("Cell phone number is valid!", "You rock dude", Alert.OK, this);
        }
    }
}

AS3 Usage example:

package be.boulevart.as3.playfield
{
    import be.boulevart.as3.validation.Email;
    import be.boulevart.as3.validation.GSM;
    import flash.display.MovieClip;
    import flash.events.*;

    public class Validation extends MovieClip
    {
        protected var emailOk:Boolean;
        protected var gsmOk:Boolean;

        public function Validation()
        {
            check_btn.addEventListener("click", doValidation);
            email_ti.tabIndex = 1;
            phone_ti.tabIndex = 2;
            check_btn.tabIndex = 3;
        }

        private function doValidation(evt:Event):void
        {
            emailOk = Email.validate(email_ti.text);
            gsmOk = GSM.validate(phone_ti.text);

            if(!emailOk)
            {
                trace("Invalid email address!");
                email_result.text = "Invalid email!";
            }
            else
            {
                trace("Email address is valid!");
                email_result.text = "Valid email!";
            }

            if (!gsmOk)
            {
                trace("Invalid cell phone number!");
                phone_result.text = "Invalid phone!";
            }
            else
            {
                trace("Cell phone number is valid!");
                phone_result.text = "Valid phone!";
            }
        }
    }
}

Enjoy! :-)

Written by Sven Dens

May 22nd, 2007 at 4:04 pm

Posted in ActionScript, Flash

6 Responses to 'Email and cell phone validation in AS2 and AS3'

Subscribe to comments with RSS or TrackBack to 'Email and cell phone validation in AS2 and AS3'.

  1. excellent, the email validator was exactly what I was looking for. thank you.

    Alex

    18 Jun 07 at 5:58 PM

  2. Very welcome Alex, glad it was of use to you! :-)

    Kind regards,
    Sven

    admin

    18 Jun 07 at 6:21 PM

  3. excellent, but has a bug, try abc@whatever.com

    (3 chars before @)

    matus

    29 Oct 07 at 5:12 PM

  4. its not completely perfect

    why won’t it validate this?

    ok: ba@xxxxxx.com
    not ok: bas@xxxxxx.com
    ok: bass@xxxxxx.com
    ok: bas@xxxxxx.nl

    Bas

    13 Dec 07 at 2:32 AM

  5. Hi, just had a look at your email validator – it doesn’t allow the apostrophe or accented characters does it? So k.o’connor@somedomain.com would fail the test.
    In fact, it doesn’t like my email address either. It doesn’t seem to like email addresses where the first part is only 3 chars long, which is the case with me.
    Hope you find this helpful.

    Dan

    13 Dec 07 at 6:33 PM

  6. this seems to fix it

    charAt(i) instead of charAt(j)!

    private static function checkFirstLevelDomainChars (s:String, i:Number, l:Number):Boolean {
    while (i

    Bas

    20 Dec 07 at 3:17 PM

Leave a Reply