AS3 Form Validation – email address and filled field
Posted on 16. Apr, 2009 by thornyeternity in ActionScript 3 Snippets
Check for a valid email address in a text input field by checking for @, fullstop and positioning of these:
private function checkEmail(whichObj:String):Boolean{
//check for @
//check for .
//check @ preceeds last dot
//check that @ is not first
//email cannot have any spaces
if(whichObj.indexOf(“@”) == -1 || whichObj.indexOf(“@”) == 0){ //not present or first
emailBool = false;
}else{
emailBool = true;//if @ exists check for dot
if(whichObj.lastIndexOf(“.”) == -1 || whichObj.lastIndexOf(“.”) == whichObj.length-1){ //not present or at very end
emailBool = false;
}else{
emailBool = true;//if both are true check @ preceeds dot
if(whichObj.lastIndexOf(“.”) < whichObj.indexOf(“@”) || whichObj.lastIndexOf(“.”) == whichObj.indexOf(“@”)+1 ){//not before and not directly after
emailBool = false;
}else{
emailBool = true;
//lastly check it has NO spaces in it
for(var i:Number = 0 ; i < whichObj.length ; i++){
if(whichObj.charAt(i) == ” “){ //if the character is a space
emailBool = false;
}
}
}
}
}
trace(“emailBool: ” + emailBool);
return emailBool;
}//checkEmail
————-
Check a text input field is filled in and it has NO spaces:
private function doFormCheck(event:MouseEvent):void{
checkName(form1_mc.name_txt.text);
trace(stringBool);
}//doFormCheck
private function checkName(whichObj:String):Boolean{
//check that its not empty only AND that its not just spaces
if(whichObj != “”){
//not empty so
stringBool = true;
for(var i:Number = 0 ; i < whichObj.length ; i++){
if(whichObj.charAt(i) != ” “){ //if the character is not a space
stringBool = true;
return stringBool;//quit checking
}else{//else it is a space
stringBool = false;
return stringBool;
}
}//for
return stringBool;
}else{
stringBool = false; //else it is empty
return stringBool;
}//if
}//checkName


