5 Replies Latest reply on Jul 26, 2006 7:46 AM by Craig Grummitt

    Check fields before submit

    philzzz
      Hi, I have a contact page on my website. The submit button checks if some fields are empty and if so displays a message to "please fill in all information boxes". However, I would like Flash to verify that there is a "@" sign, a ".com" or a ".ca" in the e-mail field to make sure the users put in an email address.

      Thank you very much.
        • 1. Re: Check fields before submit
          Craig Grummitt Level 3
          use String.indexOf to test whether a value exists within your string. If it does not exist, String.indexOf returns -1. If it does exist, it returns the position of the value.

          eg.

          if (emailAddress.indexOf("@")>-1) {
          //there is an @ symbol inside the string emailAddress
          }
          • 2. Re: Check fields before submit
            philzzz Level 1
            Hi, thanks for helping! Not quite sure how to use it thought.. This coding seems not to be working...

            frame 2 displays a message to the user saying to fill in all boxes correctly.

            • 3. Re: Check fields before submit
              Craig Grummitt Level 3
              close. try:

              (eq is deprecated in favour of ==)
              • 4. Re: Check fields before submit
                philzzz Level 1
                Hi, i dont know why but this seems to work:

                else if (eemail.indexOf("@")<1 or eemail.indexOf(".ca")<1 and eemail.indexOf(".com")<1) {

                Any explanations? No other setup works....
                • 5. Re: Check fields before submit
                  Craig Grummitt Level 3
                  sorry you're right i made a mistake there.
                  in english i wrote:

                  if the form is wrong for these reasons, tell them it's wrong.
                  otherwise if the email address is correct, tell them it's wrong.
                  otherwise, its all good - let's process it...

                  hmm... bit of a glaring error in the middle sentence when you write it out like that. so anyway, to resolve this, you could either simply add a 'not' to the middle sentence:

                  otherwise if the email address is NOT correct, tell them it's wrong.

                  or, in code form:

                  } else if (!(eemail.indexOf("@")>-1 and (eemail.indexOf(".com")>-1 or eemail.indexOf(".ca")>-1))) {

                  or you could convert the statement to become:

                  } else if (eemail.indexOf("@")==-1 or (eemail.indexOf(".com")==-1 and eemail.indexOf(".ca")==-1)) {

                  which in reality is pretty close to what you had anyway. && (and) has a higher operator precedence than || (or) so the brackets aren't necessary.

                  an "@" symbol or a ".com" or ".ca" in the first character of a string still wouldn't make a valid email address, so your suggestion of checking that these characters are at least in the second character of the string makes sense.

                  Which all is a long-winded explanation of why the code you've described works!