1 Reply Latest reply on Dec 13, 2007 7:23 AM by robdillon

    Validating Form Fields

    ScottE
      Hi All,

      I am using Flash 8;ActionScript 2.0.

      I have a form with serveral input fields. I want to validate these fields. For example make sure the email input is in the correct format, that the telephone input has only numbers, etc.

      All of the tutorials I have looked at attach the AS to the submit button. I do not want to do this. What I'd like to set up is have AS check the input field entry to make sure it's correct PRIOR to the submission of the form.

      Is this possible?

      This is the AS for the form with no validation:

      tl = this;
      initF();
      stop();

      //Tabs

      nameTF.tabIndex = 1
      emailTF.tabIndex = 2
      companyTF.tabIndex = 3
      phoneTF.tabIndex = 4
      messageTF.tabIndex = 5

      //End Tabs

      sendBtn.onRelease = function() {
      if (checkFormF()) {
      sendEmailF();
      } else {
      tl.gotoAndStop("errorFrame");
      }
      };
      function checkFormF():Boolean {
      if (nameTF.text == "Name" || companyTF.text == "Company" || emailTF.text == "E-mail" || phoneTF.text == "Phone" || messageTF.text == "Message" || nameTF.text == "" || companyTF.text == "" || emailTF.text == "" || phoneTF.text == "" || messageTF.text == "") {
      return false;
      } else {
      return true;
      }
      }
      function sendEmailF() {
      sendLV = new LoadVars();
      receiveLV = new LoadVars();
      receiveLV.onLoad = function(s) {
      if (s) {
      tl.gotoAndStop("thankyouFrame");
      }
      };
      sendLV.name1 = nameTF.text;
      sendLV.email = emailTF.text;
      sendLV.message1 = messageTF.text;
      sendLV.phone = phoneTF.text;
      sendLV.company = companyTF.text;
      sendLV.sendAndLoad("mailer5.php",receiveLV,"POST");
      }
      function initF() {
      nameTF.text = "Name";
      companyTF.text = "Company";
      emailTF.text = "E-mail";
      phoneTF.text = "Phone";
      messageTF.text = "Message";
      }
      label_01 = "Name";
      label_02 = "Email";
      label_03 = "Company";
      label_04 = "Phone";
      label_05 = "Message";

      nameTF.text = label_01;
      emailTF.text = label_02;
      companyTF.text = label_03;
      phoneTF.text = label_04;
      messageTF.text = label_05;
      //
      this.onEnterFrame = function() {
      nameTF.onSetFocus = function() {
      if (nameTF.text == label_01) {
      nameTF.text = "";
      }
      };
      nameTF.onKillFocus = function() {
      if (nameTF.text == "") {
      nameTF.text = label_01;
      }
      };
      //
      emailTF.onSetFocus = function() {
      if (emailTF.text == label_02) {
      emailTF.text = "";
      }
      };
      emailTF.onKillFocus = function() {
      if (emailTF.text == "") {
      emailTF.text = label_02;
      }
      };
      //
      companyTF.onSetFocus = function() {
      if (companyTF.text == label_03) {
      companyTF.text = "";
      }
      };
      companyTF.onKillFocus = function() {
      if (companyTF.text == "") {
      companyTF.text = label_03;
      }
      };
      //
      phoneTF.onSetFocus = function() {
      if (phoneTF.text == label_04) {
      phoneTF.text = "";
      }
      };
      phoneTF.onKillFocus = function() {
      if (phoneTF.text == "") {
      phoneTF.text = label_04;
      }
      };
      //
      messageTF.onSetFocus = function() {
      if (messageTF.text == label_05) {
      messageTF.text = "";
      }
      };
      messageTF.onKillFocus = function() {
      if (messageTF.text == "") {
      messageTF.text = label_05;
      }
      };
      };

      Your help is much appreciated,
      Scott
        • 1. Re: Validating Form Fields
          robdillon Most Valuable Participant
          I don't have a solution for you. The reason that the existing solutions that you've found use the submit event is quite simple. It's the least intrusive place to check for errors. You have a limited number of events where you can place this content checking code. If you wanted to test the email address that the user entered after that entry was made, how would you do that? You can tell when the user gives focus to the textfield, you can tell when the user enters a character, you can tell when the textfield loses focus. None of these events is really appropriate for testing the content. What is the problem with waiting until the submit button is used?