A simple check for a valid email address, makes sure it has an ‘@’ and a ‘.’

public function checkEmail(_email:String):Boolean {
var passCheck:Boolean = false
var atLoc:int = _email.indexOf(“@”)
var dotLoc:int = _email.indexOf(“.”)
if (atLoc > 0 && dotLoc >0) {
passCheck =true
}
return passCheck
}

Well thanks to Jon, I’ve now looked into a method using Regular Expressions which seems to be a whole bunch better. Sadly the Adobe help was crappy and I instead found some working source here

function isValidEmail(email:String):Boolean {
var EMAIL_REGEX : RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
return Boolean(email.match(EMAIL_REGEX)));

}

3 Responses to “AS3 Rough check for valid email”

  1. Jon says:

    Surely a regular expression function would be easier especially as AS3 supports regular expressions? Probably less code than this and more accurate.

  2. Jon says:

    function isValidEmail(email:String):Boolean {
    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    return emailExpression.test(email);
    }

  3. Marcus says:

    Hi, I am very new to regular expressions. This looks great. Unfortunately this pattern allows a dot right before the @ which is not allowed. I don’t know what must be added to correct the pattern.. do you? I’d be very interested in it.

    Cheers
    Marcus

Leave a Reply