1 Reply Latest reply on Sep 10, 2007 11:01 PM by bayani.portier

    Validating multiple Emails

    navi1084
      Hi,
      I am new user to adobe flex. I am facing a problem in E-Mail id validation. Single e-mail id i can validate successfully. But i cannot validate multiple email id's sepeprated by commas in the text area. i need help on this.Following is my code for validate

      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.controls.Alert;
      import mx.validators.EmailValidator;
      private var emailValidator:EmailValidator;
      private function creationValidation():void
      {
      var emailAddr:String = new String();
      emailAddr= email.text;
      var emailId:Array = new Array();
      emailValidator=new EmailValidator();
      emailValidator.source= email;
      emailValidator.property="text";
      }



      I need to call a function only if all the e-mail id's are valid

      Thank u in advance
        • 1. Re: Validating multiple Emails
          bayani.portier
          Hi,

          here's a good about doing this.

          Your mx validators are a pain to use, so dive into actionscript.

          Here is the code for it. I would suggest googling a better Regex than what is there. Just in case, a Regex is a set of characters that define the rules for whether a string is valid.

          I'll monitor the thread if you have any questions.

          b

          ---

          public function initTest():void
          {
          //define email strings
          var myEmailSet:String = "abc123@test, blah@test.com, moo@test.com";

          if(checkEmailValid(myEmailSet))
          {
          trace("All correct");
          }
          else
          {
          trace("Invalid Email(s)");
          }
          }

          public function checkEmailValid(myEmailSet:String):Boolean
          {
          //Deliminate the Email addresses from each other
          var emails:Array = myEmailSet.split(',');
          var allAddressesValid:Boolean=true;

          //Regex pattern - you probably want to google for a better one
          var pattern : RegExp = /[A-Za-z0-9_]+@[A-Za-z0-9_]+\.[A-Za-z]+/;

          //temporary variable for the individual email address within the array
          var email:String;

          //loop for each email in the array
          for(var loop:int = 0; loop<emails.length; loop++)
          {
          //make sure it's not empty, or you will get null errors and general explosions of doom
          if(emails[loop]!="")
          {
          //set the array value to a local string variable, makes it easier to manipulate
          email=emails[loop].valueOf();

          //if the email address has a resultant space in front of it after being split by the comma
          if(email.charAt(0)==" ")
          {
          //trim the email, see the function below
          email=trimFront(email, " ");
          }

          //check the email against the Regex pattern
          if( pattern.exec( email ) != null )
          {
          //run the valid email function
          functionIfValid(email);
          }
          else
          {
          //set the valid addresses flag to false
          allAddressesValid=false;
          }
          }
          else
          {
          //set the valid addresses flag to false
          allAddressesValid=false;
          }
          }

          //return if there were any malformed emails in the set
          return allAddressesValid;
          }

          public function functionIfValid(email:String):void
          {
          //do whatever your function does
          trace("Valid Address: " + email);
          }

          public function trimFront(str:String, char:String):String
          {
          //turns the string into a single character
          char = stringToCharacter(char);

          //check to see if the characters are the same
          if (str.charAt(0) == char)
          {
          //calls the trimfront function recursively, and adds it to the string
          str = trimFront(str.substring(1), char);
          }
          return str;
          }

          public function stringToCharacter(str:String):String {
          if (str.length == 1)
          {
          return str;
          }
          return str.slice(0, 1);
          }