8 Replies Latest reply on Jan 7, 2013 1:44 PM by Rothrock

    Validating a batch of up to three email addresses

    Germaris

      Hi there!

       

      Users of my site have a main email address and, sometimes one or two alternate email addresses.

      Until then, they had of course, the ability to change their main address.

      But now I wish to offer them the ability to have up to two alternate addresses.

      The interface shows, in order:

      a - the active main address

      b - the first active alternate (if any)

      c - the second active alternate (if any)

      User can enter up to three addresses in three input fields displayed alongside those showing the currently used addresses

      All three addresses must be different from each other.

      Until then, nothing very difficult as I had only one email address to verify.

      But now there are three of them to verify!

       

      Names of the two input fields for the alternate addresses are: newFirstAltFld and new SecondAltFld

       

      How can I achieve this task?

       

      Many thanks in advance for your help!

       

      PS: Sorry for the strings in french, but it doesn't matter anyway (I hope so!)

       

       

       

      Code I use for now:

      // define an array to store the errors found while using the validateEmail function

      var errorsValidateEmail:Array = new Array();

      // this function is to validate email address

      validateEmailFct = function () {

          if (newEmailFld.text.indexOf("@") < 2) {

              errorsValidateEmail.push("<font color='#FF0000'>- Le symbole \"<b>@</b>\" est manquant ou mal placé.</font>");

          }

          if (newEmailFld.text.lastIndexOf(".") <= (newEmailFld.text.indexOf("@") + 2)) {

              errorsValidateEmail.push("<font color='#FF0000'>- Un point \"<b>.</b>\" est manquant ou mal placé.</font>");

          }

          if (newEmailFld.text.length < 8) {

              errorsValidateEmail.push("<font color='#FF0000'>- L'adresse fournie est trop courte.</font>");

          }

      };

      // validating email

      validateFormFct = function () {

          errorsValidateEmail.length = 0;

          validateMailFct();

          if (errorsValidateEmail.length > 0) {

              errorMsgFld.html = true;

              errorMsgFld.htmlText = "<font color='#FF0000'>Les erreurs suivantes subsistent :<br /></font>";

              var i = -1;

              while (++i < errorsValidateEmail.length) {

                  errorMsgFld.html = true;

                  errorMsgFld.htmlText += errorsValidateEmail[i] + newline;

              }

          }

          else {

              verifEmailFct(); // to verify domain name and validity (if already used or if not valid) of new email address, not shown here but it works, I swear!

          }

      };

        • 1. Re: Validating a batch of up to three email addresses
          Ned Murphy Adobe Community Professional & MVP

          In your form validation function you will need to validate each email address thru the validateMailFct(); function.  If it involves three different textfields, then you could pass the textfields into the function as an argument and test that way.  You could place the three fields into an array so that you can target them using a loop.   

           

          If you need to confirm that no two are the same, then you can create another function that tests each against the other for equality, as in.... if  1 == 2 || 1 == 3 || 2 == 3 then there's a problem.

           

          It will probably be a good idea to check if an entry exists before trying to test it.

          1 person found this helpful
          • 2. Re: Validating a batch of up to three email addresses
            Germaris Level 1

            Thanks Ned for your reply.

             

            Since I post my question, I modified the validateEmailFct as follow:

             

            // define an array to store the errors found while using the validateEmail function

            var errorsValidateEmail:Array = new Array();

            // this function is to validate email addresses in the three fields

            validateMailFct = function () {

                if (newEmailFld.text.indexOf("@") < 2) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Le symbole \"<b>@</b>\" est manquant ou mal placé.(Adresse principale)</font>");

                }

                if (newEmailFld.text.lastIndexOf(".") <= (newEmailFld.text.indexOf("@") + 2)) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Un point \"<b>.</b>\" est manquant ou mal placé.(Adresse principale)</font>");

                }

                if (newEmailFld.text.length < 8) {

                    errorsValidateEmail.push("<font color='#FF0000'>- L'adresse fournie est trop courte.(Adresse principale)</font>");

                }

                if (newFirstAltEmailFld.text.indexOf("@") < 2) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Le symbole \"<b>@</b>\" est manquant ou mal placé.(Alternative 1)</font>");

                }

                if (newFirstAltEmailFld.text.lastIndexOf(".") <= (newEmailFld.text.indexOf("@") + 2)) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Un point \"<b>.</b>\" est manquant ou mal placé.(Alternative 1)</font>");

                }

                if (newFirstAltEmailFld.text.length < 8) {

                    errorsValidateEmail.push("<font color='#FF0000'>- L'adresse fournie est trop courte.(Alternative 1)</font>");

                }

                if (newSecondAltEmailFld.text.indexOf("@") < 2) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Le symbole \"<b>@</b>\" est manquant ou mal placé.(Alternative 2)</font>");

                }

                if (newSecondAltEmailFld.text.lastIndexOf(".") <= (newEmailFld.text.indexOf("@") + 2)) {

                    errorsValidateEmail.push("<font color='#FF0000'>- Un point \"<b>.</b>\" est manquant ou mal placé.(Alternative 2)</font>");

                }

                if (newSecondAltEmailFld.text.length < 8) {

                    errorsValidateEmail.push("<font color='#FF0000'>- L'adresse fournie est trop courte.(Alternative 2)</font>");

                }

            };

             

            which is a repetition of testing for the other two alternate fields.

             

            I'm conscious this is not very smart but I don't know how to simplify this process with an array as you suggest... (quote: "You could place the three fields into an array so that you can target them using a loop.").

             

            About your second paragraph (which demonstrates you have fully understood the whole problem), the answer is yes.

            I need to confirm that no two are the same, and I already planned to check if an entry exists before trying to test it.

             

            Regards,

             

            Gerard

            • 3. Re: Validating a batch of up to three email addresses
              Ned Murphy Adobe Community Professional & MVP

              There is nothing "not very smart" about what you have done.  Anyone who tries to solve something rather than hoping to have it handed to them is doing something very smart...  that's the best way to learn as I see it.

               

              Just as an example of one way of implementing an array/loop approach...

               

              var fields = new Array(newEmailFld, newFirstAltEmailFld, newSecondAltEmailFld);

              var fieldIDs = new Array("Adresse principale", "Alternative 1", "Alternative 2");

               

              validateMailFct = function () {

                  for(i=0; i<fields.length; i++){

                      if (fields[i].text.indexOf("@") < 2) {

                          errorsValidateEmail.push("<font color='#FF0000'>- Le symbole \"<b>@</b>\" est manquant ou mal placé.("+fieldIDs[i]+")</font>");

                      }

                      if (fields[i].text.lastIndexOf(".") <= (fields[i].text.indexOf("@") + 2)) {

                          errorsValidateEmail.push("<font color='#FF0000'>- Un point \"<b>.</b>\" est  manquant ou mal placé.("+fieldIDs[i]+")</font>");

                      }

                      if (fields[i].text.length < 8) {

                          errorsValidateEmail.push("<font color='#FF0000'>- L'adresse fournie est trop courte.("+fieldIDs[i]+")</font>");

                      }

                  }

              }

              • 4. Re: Validating a batch of up to three email addresses
                Germaris Level 1

                I agree with your opinion. For years I've been learning AS 1.0 then 2.0 from scratch, using the manuals, then the online Help, books I bought from Macromedia Press and of course the very valuable help of people like you on many different forums.

                 

                The example you wrote is crystal clear! Thanks!

                But what if one or two (or three fields !!!) are empty?

                 

                I forgot to mention that nothing is mandatory in this form.

                User is totally free to do what he/she want.

                For example, user is able to change the first alternate while leaving the two other fields blanks. So there's no need to give him useless validating error messages about those fields.

                 

                In another way, I would say: How to skip empty fields?

                (by the way, in your first reply you wrote: "quote: It will probably be a good idea to check if an entry exists before trying to test it.")

                • 5. Re: Validating a batch of up to three email addresses
                  Ned Murphy Adobe Community Professional & MVP

                  "(by the way, in your first reply you wrote: "quote: It will probably be a good idea to check if an entry exists before trying to test it.")"

                   

                  Your response to that indicated you planned to do that, so I got the impression you knew what to do...

                   

                  To check for non-entries you could test the length property of each textfield as a surrounding conditional for the other tests...

                   

                   

                  validateMailFct = function () {

                      for(i=0; i<fields.length; i++){

                        if(fields[i].length > 0){

                   

                            // all the rest of the conditional tests

                   

                        }

                      }

                  }

                   

                  As long as the textfield is not set up to be mutliline, the "fields[i].length > 0" should work.  If it happens to be multiline then you probably need to change it to be "fields[i].length > 1" instead

                  1 person found this helpful
                  • 6. Re: Validating a batch of up to three email addresses
                    Germaris Level 1

                    Thank you, Ned.

                    I understand very well...

                    It's just another conditional added, related to fields length, which commands all the other conditionals.

                    So simple... Why the h... I didn't find myself this solution?!

                     

                    About the fields properties, they all are set up as single line fields.

                     

                    2013 is so close, hope you'll begin it with joy!

                     

                    Best regards,

                     

                    Gerard

                    • 7. Re: Validating a batch of up to three email addresses
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome Gerard.  I hope the new year brings good stuff for all of us.

                      • 8. Re: Validating a batch of up to three email addresses
                        Rothrock Level 5

                        You might want to look into Regular Expressions. They are built into AS3, but for AS2 you will need to get one. I've used this and it is good:

                         

                        http://www.jurjans.lv/flash/RegExp.html

                         

                        And then find a regular expression that validates your emails.

                         

                        http://www.regular-expressions.info/email.html

                        http://www.linuxjournal.com/article/9585

                        1 person found this helpful