28 Replies Latest reply on Apr 26, 2011 2:51 PM by DC Dowd

    Javascript that affects all text fields

    CompTerra

      I am now trying to figure out a script that will affect all text fields in the document, there would be a button that when clicked would affect all text fields in the document.

       

      Thank you

        • 1. Re: Javascript that affects all text fields
          try67 MVP & Adobe Community Professional

          Can't be done without knowing their names.

          • 2. Re: Javascript that affects all text fields
            CompTerra Level 1

            I am using the following now: this.getField(["TEXT"]); that affects all fields starting with TEXT...my issue is that not all fileds may start with TEXT.

            • 3. Re: Javascript that affects all text fields
              try67 MVP & Adobe Community Professional

              Are you 100% sure about that? It's not documented and I just tried it on a

              form and it just returns NULL.

              • 4. Re: Javascript that affects all text fields
                CompTerra Level 1

                100% positive...notice the [ inside the " in this.getField(["TEXT"])  I started with naming the field: this.getField("TEXT.0.1.2") then found I could do the [TEST] inside quotes and that will affect all fields starting with TEXT...text.0 text.0.1 text.0.0.1, and so on.

                • 5. Re: Javascript that affects all text fields
                  try67 MVP & Adobe Community Professional

                  I did, but what you are accessing are actually copies of the same field

                  ("widgets"), not (strictly speaking) separate fields.

                  If you will remove the periods from their names it will not work anymore.

                  • 6. Re: Javascript that affects all text fields
                    CompTerra Level 1

                    That makes sense...

                     

                    I do have many text fields and each one starts with text.0 and so on, so I am lucky for that, though the issue continues with those fields that may not have that name.

                    • 7. Re: Javascript that affects all text fields
                      try67 MVP & Adobe Community Professional

                      Like I said, those fields are copies of one another. If you change the value

                      of one of them, the rest will change as well.

                      • 8. Re: Javascript that affects all text fields
                        CompTerra Level 1

                        When you say value what do you mean?  If I type the text 1234 into TEXT.0 then proceed to another field named TEXT.0.1 and type the text 5432 both values of those fields are different....I am confused in what your saying.  Each field is its own field, had I named two fields TEXT.0 then both fields are the same and if I type into one it will show in both.

                        • 9. Re: Javascript that affects all text fields
                          try67 MVP & Adobe Community Professional

                          But if you create TEXT.0 and TEXT.1 they will have the same value.

                          • 10. Re: Javascript that affects all text fields
                            CompTerra Level 1

                            Same value as in I copy TEXT.0 and paste somehwere then rename to TEXT.1? That would retain the original value of course until I delete or change it.  However if I create TEXT.0 then create TEXT.1 they are both seperate fields and do not seem to retain any of the same values.  I just did it to make sure....

                            • 11. Re: Javascript that affects all text fields
                              try67 MVP & Adobe Community Professional

                              You're right, it doesn't. It looks like it "tricks" Acrobat to think these

                              are widgets of the same field, when in fact the original field doesn't

                              exist.

                              • 12. Re: Javascript that affects all text fields
                                CompTerra Level 1

                                This is lucky for me....at least I have the "[TEXT]" option that will address all fields starting with that..

                                 

                                Thank you for your help.....if you can think of anything let me know!!

                                • 13. Re: Javascript that affects all text fields
                                  try67 MVP & Adobe Community Professional

                                  By the way, you don't need the square brackets. getField("TEXT") should work

                                  as well.

                                  • 14. Re: Javascript that affects all text fields
                                    MarkWalsh Level 4

                                    Is this what you are looking for?

                                     

                                    function processTextFields () {

                                     

                                         for(var i=0;i<this.numFields;i++) {

                                              // Loop through all fields on document

                                              var fldName = this.getNthFieldName(i);

                                              var oField = this.getField(fldName);

                                     

                                              var a = oField.getArray();

                                              for (j =0; j < a.length; j++) {

                                                   if (oField.type == 'text') {

                                                        // Do something here

                                                   }

                                              }

                                         }

                                    }

                                     

                                    • 15. Re: Javascript that affects all text fields
                                      CompTerra Level 1

                                      I have not tried yet....this will affect all text fields?  I want to be able to click a button and have all text fields affected with the proceeding action.

                                      • 16. Re: Javascript that affects all text fields
                                        try67 MVP & Adobe Community Professional

                                        Nice one. I totally forgot about this...

                                        • 17. Re: Javascript that affects all text fields
                                          MarkWalsh Level 4

                                          CompTerra wrote:

                                           

                                          I have not tried yet....this will affect all text fields?  I want to be able to click a button and have all text fields affected with the proceeding action.

                                           

                                          Yes. The first loop:

                                           

                                               for(var i=0;i<this.numFields;i++) {

                                           

                                           

                                          will loop thorugh all of the fields in the document. The second loop:

                                           

                                           

                                                    for (j =0; j < a.length; j++) {

                                           

                                           

                                          will loop through all of the widgets for that field. You can eliminate the second loop if you don't need to work with the actual widgets.

                                           

                                           

                                          You can change the If statement to something else if you want to process other types of fields (i.e. combo boxes)

                                          • 18. Re: Javascript that affects all text fields
                                            DC Dowd

                                            Could this script be modified to validate all fields in a form before  submitting the form by e-mail by doing something akin to what follows?  I'm trying to validate fields before using the submitForm function  without using Adobe's built-in Submit button because I want to be able  to pull form fields to populate the subject line of the e-mail and have  been unable to do so with the Submit button (I'm guessing it doesn't  allow for code in the "?subject="portion).

                                             

                                            function processTextFields () {

                                             

                                                 for(var i=0;i<this.numFields;i++) {

                                                      // Loop through all fields on document

                                                      var fldName = this.getNthFieldName(i);

                                                      var oField = this.getField(fldName);

                                             

                                                      var a = oField.getArray();

                                                      for (j =0; j < a.length; j++) {

                                                      if (oField.type == "") {

                                                       noreturn;

                                                      } else {

                                                      return; }

                                                 };

                                             

                                            this.mailDoc({
                                            bUI: false,
                                            cTo: "address@example.com",
                                            cSubject: "Form" + " " + this.getField("DesiredField01").value + " " + this.getField("DesiredField02").value + " " + this.getField("Date").value,
                                            });

                                            • 19. Re: Javascript that affects all text fields
                                              try67 MVP & Adobe Community Professional

                                              Of course you can set the subject. You're doing so in your own code by setting the value of cSubject.

                                               

                                              If you want to condition sending the email on on the text fields being filled-in, use the loop described earlier in this form to check the value of the fields.

                                              If any of them is empty, exit the function by calling "return;", otherwise the script will continue to the line that sends the email.

                                              Be aware that there's no such command as "noreturn" in JS.

                                              • 20. Re: Javascript that affects all text fields
                                                DC Dowd Level 1

                                                Right, then. Here's the code I'm inserting into my document.

                                                function processTextFields () {

                                                 

                                                     for(var i=0;i<this.numFields;i++) {
                                                          // Loop through all fields on document
                                                          var fldName = this.getNthFieldName(i);
                                                          var oField = this.getField(fldName);

                                                 

                                                          var a = oField.getArray();
                                                          for (j =0; j < a.length; j++) {
                                                          if (oField.type == "") {
                                                          return;
                                                          } else {
                                                this.mailDoc({
                                                bUI: false,
                                                cTo: "ddowd@capstoneturbine.com",
                                                cSubject: "Form" + " " + this.getField("SiteName01").value + " " + this.getField("SysSN").value + " " + this.getField("Date").value,
                                                });}
                                                     };}}

                                                 

                                                I'm getting nothing when the required fields are not filled in, which I rather expected. However, when all fields are filled in I am still getting no response from the button. The mailDoc bit of code is working fine with no other code attached, but every attempt at validating the form's required fields is causing trouble.

                                                 

                                                A thought occurs to me (I'm new at JS coding), but this code is referencing EVERY field in the document, right? This would mean I should get no result even if the non-required fields are filled out (if my understanding is correct). Can this code be modified to only apply to required fields? Thanks for the help in getting this far.

                                                 

                                                Using Acrobat 9.

                                                • 21. Re: Javascript that affects all text fields
                                                  try67 MVP & Adobe Community Professional

                                                  You have quite a few mistakes in your code. Use this instead:

                                                   

                                                  function processTextFields () {
                                                      for (var i=0; i<this.numFields; i++) {
                                                          // Loop through all fields on document
                                                          var fldName = this.getNthFieldName(i);
                                                          var oField = this.getField(fldName);
                                                          // check if the field is a text field, and if it's empty
                                                          if (oField.type == "text" && oField.value=="") {
                                                              return;
                                                          }
                                                      }
                                                     
                                                      // this part is only executed if all the text fields are not empty
                                                      this.mailDoc({
                                                          bUI: false,
                                                          cTo: "ddowd@capstoneturbine.com",
                                                          cSubject: "Form" + " " + this.getField("SiteName01").value + " " + this.getField("SysSN").value + " " + this.getField("Date").value
                                                      });
                                                  }

                                                  • 22. Re: Javascript that affects all text fields
                                                    DC Dowd Level 1

                                                    Copied and pasted that code to avoid further mistakes. Still not getting anything, even when all text fields are filled in. Adobe gives no error in the code, just doesn't run it when I extend features and open the form in Reader (which is where those who will use the form will be opening it).

                                                     

                                                    I keep shuddering at the thought that someone is going to say I should use the built-in "Submit button" option, which would validate all required fields without the hassle. However, that feature is exactly where I am having trouble getting the subject line of my e-mail to pull the contents of fields in the form.

                                                     

                                                    Thanks for the code help. Going to keep chipping away at it and running through the API for more insight.

                                                    • 23. Re: Javascript that affects all text fields
                                                      try67 MVP & Adobe Community Professional

                                                      Are you calling the function from anywhere?

                                                      • 24. Re: Javascript that affects all text fields
                                                        DC Dowd Level 1

                                                        The script is being run on the mouse up of a button that is being used to submit the form.

                                                         

                                                        Not being called from anywhere, just being run when that button is clicked.

                                                        • 25. Re: Javascript that affects all text fields
                                                          try67 MVP & Adobe Community Professional

                                                          Yes, but you must call the function, or the code within it won't execute.

                                                          Add this as the last line, after everything else:

                                                           

                                                          processTextFields();

                                                          • 26. Re: Javascript that affects all text fields
                                                            DC Dowd Level 1

                                                            Excellent. That works perfectly.

                                                             

                                                            Now to look into validating only required fields.

                                                            • 27. Re: Javascript that affects all text fields
                                                              MarkWalsh Level 4

                                                              Change

                                                                      if (oField.type == "text" && oField.value=="") {

                                                               

                                                              to
                                                                      if (oField.type == "text" && oField.value=="" && oField.required) {
                                                              (Note - since this is only checking Text fields, you will need to make changes if you want it to validate required fields that are not textfields - e.g. radio buttons, checkboxes, etc.)
                                                              • 28. Re: Javascript that affects all text fields
                                                                DC Dowd Level 1

                                                                Excellent!

                                                                 

                                                                Perfect. Works exactly as I need it to work. This forum rocks.

                                                                 

                                                                Thank you for your help.