04
Feb
08

Email Validation in AS2

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I Have just posted up a code snippet that validates general email addresses in AS2. [Update] - Click here for the AS3 Email Validation Code

Full code after the jump.

function validateEmail(emailStr:String) {
var errorCount:Number = 0;
//check to see if there are at least two chars be for the @ symbol
if (emailStr.indexOf("@")<2) {
errorCount++;
}
//check that the @ is not within 2 chars of .
if (emailStr.lastIndexOf(".")<=(emailStr.lastIndexOf("@")+2)) {
errorCount++;
}
//check length eg min aa@bb.cc
if (emailStr.length<8) {
errorCount++;
}
//check there is only one @
if (emailStr.indexOf("@") != emailStr.lastIndexOf("@")) {
errorCount++;
}
// return a T/F to the flash file
if (errorCount == 0) {
return true;
} else {
return false;
}
}

//to use it
if(validateEmail(email.text)){
trace("EMAIL VALID");
}else{
trace("EMAIL NOT VALID")
}

Found this useful? How about buying me a coffee - simply click here.


3 Responses to “Email Validation in AS2”


  1. 1 Vagassy Mar 2nd, 2008 at 9:38 pm

    It’s very useful!

    Thank you!

  2. 2 Kristian Mar 28th, 2008 at 11:21 am

    Your script will recognize most common email-adresses, but will filter out a lot of completely valid email adresses, AFAICS.

    For instance, a@something.dk, or bob.a@domain.com, which are perfectly legal according to RFC.

    Regards
    Kristian

  3. 3 admin Apr 10th, 2008 at 5:13 am

    @Kristian thanks for the heads up. will look into it

Leave a Reply




Close
E-mail It