AmitHarpaz
AmitHarpaz

Reputation: 23

FLASH AS2 - small issue with email validation

I'm new to FLASH and I need to validate an email address in a form. my code is:

validate_btn.onRelease = function() { 
     indexOfAt = email.text.indexOf("@"); 
     lastIndexOfDot = email.text.lastIndexOf("."); 
     if (indexOfAt !=-1 && lastIndexOfDot !=-1){ 
          if (lastIndexOfDot <indexOfAt) { 
               message.text="please verify your email."; 
          }else { 
               message.text="Your email seems okay"; 
          } 
     } else { 
          message.text="please enter correct email address"; 
     } 
}

It seems to work fine the only problem is that follow email is accepted: myEmail@domain. and @domain.com does anyone can help me fix it? take into consideration that is it AS2 so I can't use RegExp.

Thanks and sorry for my bad English.

Upvotes: 0

Views: 690

Answers (2)

Pieter Willaert
Pieter Willaert

Reputation: 879

http://www.actionscript.org/forums/showthread.php3?t=77605 here it is said there is a solution to get regex working in AS2

otherwise you can try

if (
    (emailString.indexOf("@") > 0) &&
    (emailString.lastIndexOf(".") > (emailString.indexOf("@") + 1)) &&
    (emailString.lastIndexOf(".")
){
    //valid(ish)
}

Upvotes: 0

FrancescoMM
FrancescoMM

Reputation: 2960

You also have to test for

indexOfAt>0

and

lastIndexOfDot<email.text.length-2

Please note that this is a trial, code is UNTESTED

validate_btn.onRelease = function() { 
     indexOfAt = email.text.indexOf("@"); 
     lastIndexOfDot = email.text.lastIndexOf("."); 
     if (indexOfAt>0 && lastIndexOfDot !=-1 && lastIndexOfDot<email.text.length-2){ 
          if (lastIndexOfDot <indexOfAt) { 
               message.text="please verify your email."; 
          }else { 
               message.text="Your email seems okay"; 
          } 
     } else { 
          message.text="please enter correct email address"; 
     } 
}

Upvotes: 1

Related Questions