4 Replies Latest reply on Jul 27, 2010 11:07 AM by adobe_new1

    Need to make a field required only if another text field has been filled out

    adobe_new1

      Greetings everyone!

       

      I'm putting together a new staff form and I need to make it so that if you fill in data in the 'new employee' text field, then another text field asking 'where is the new employee to sit, is required.  So far, I know how to make the required field property work, but it holds up my entire form if I'm not filling in a certain section - which is not what I want. 

       

      This seems really easy, I'm just not sure how to do it in Adobe- and I'm not sure if I need scripting or just a simple validation on the fields.

       

      Any thoughts or ideas are appreciated.  Be advised, I am totally new to adobe, so you may have to go slow with me.  I do, however, understand a general- if, then, else programming style structure.

       

      Thanks in advance!

       

       

       

      ****** here is the code I'm using******

      var rgEmptyTest = /^\s*$/;
      if(rgEmptyTest.test(event.value))
      { // Field is empty, make other fields not required
      this.getField("Move FROM Program").required = false;
      }
      else
      {// Field is not empty, make other fields reqired
      this.getField("Move FROM Program").required = true;
      }

      if(rgEmptyTest.test(event.value))
      { // Field is empty, make other fields not required
      this.getField("Move TO Program").required = false;
      }
      else
      {// Field is not empty, make other fields reqired
      this.getField("Move TO Program").required = true;
      }

        • 1. Re: Need to make a field required only if another text field has been filled out
          Plstsn Level 3

          I don't see anything wrong with the code. Is the code a Document Script?

           

          Since you repeat the code I assume you have several fields that will pair with the required/not required relationship.

           

          I would type the following in the document script:

           

           

          var rgEmptyTest = /^\s*$/;


          // t is the value to be tested and f is the field to set accordingly


          function testMyField (t, f) {

               if (rgEmptyTest.test(t)) {

                    this.getField(f).required = false;         

               } else {

                    this.getField(f).required = true;         

               }

          }

           

           

          Then make a custom validation script for the 'new employee" field will be:

           

          testMyField(event.value, "Move FROM Program");

           

          And now you can quickly add a lot of other fields with little code to alter.

           

          Hope that makes sense and I've understood you correctly.

           

          Torben.

          • 2. Re: Need to make a field required only if another text field has been filled out
            adobe_new1 Level 1

            Thank you Torben.

             

            Please forgive me.  I'm not a programming pro.  I somewhat understand what you mean, but not enough to alter the code.  Can you please include your answer within the code I have so I can easily cut and paste?

             

            I know that may be asking a lot, so if you cant do it I understand.  Thanks in advance.

            • 3. Re: Need to make a field required only if another text field has been filled out
              Plstsn Level 3

              Go to Advanced > Document Processing > Document Javascript…

              In the Script name box, type a name and click 'Add'.

              Select all and delete it.

              Paste the document script:

               

              var rgEmptyTest = /^\s*$/;


              // t is the value to be tested and f is the field to set accordingly


              function testMyField (t, f) {

                   if (rgEmptyTest.test(t)) {

                        this.getField(f).required = false;        

                   } else {

                        this.getField(f).required = true;        

                   }

              }

               

              Click 'Ok' and then Click 'Close'.

               

              Now go to the field that user types into - i.e. the field that will have to be tested against your regular expression - and double click to open the properties for that field. Let's call it the typeInField just understanding purposes.

              Go to the 'Validate' tab.

              Check the 'Run custom validation script' and hit 'Edit…'

               

              Paste the other part of the script:

              testMyField(event.value, "required field");

              Substitute the text "required field"  with the name of the field in your document that should be set as Required - in your case probably "Move FROM Program".
              If you want the both the "Move FROM Program" and "Move TO Program" to be set as required when the typeInField is filled with text, just make two lines. They would look like this:
              testMyField(event.value, "Move FROM Program");
              testMyField(event.value, "Move TO Program");

               

              Hope it works out for you.

               

              Torben.