11 Replies Latest reply on Feb 27, 2017 8:26 AM by rmck7984

    Javascript - Fillable form in Acrobat

    rmck7984 Level 1

      Good morning,

            I am trying to set up a JS that allows a button to look for any missing required fields.  Text fields and radio buttons are the types of fields that are required throughout this form.  I need it to prompt all missing required fields, then once all are complete, prompt that the form is complete and ready to be printed, then followed with the print screen.  At this point, I cannot figure out how to get it to point out the radio buttons or prompt when complete.  It does pop up the message for the text fields and does pop up the print screen once "complete", however, whether the radio button options are selected or not, it shows all radio buttons in the error as if they were not completed.  There is one gender radio button selection and the rest are "yes"/"no" type of options where they have to go through and click either yes or no on all listed.  The default is set to where neither is selected.

       

       

      This is what I have so far.  Can you please let me know how this can be changed in order to do what I need it to do?  Thanks in advance.

       

      var emptyFields = [];
      for (var i=0; i<this.numFields; i++) {
           var f= this.getField(this.getNthFieldName(i));
           if (f.type!="button" && f.required ) {
                if ((f.type=="text" && f.value=="") || (f.type=="radio button" && f.value==""))
        

      f.strokeColor = color.red;   //Highlights the required fields in red

      emptyFields.push(f.name);
          }
      }

      if (emptyFields.length>0) {
           app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
      }
      else app.execMenuItem("Print");

        • 1. Re: Javascript - Fillable form in Acrobat
          gkaiseril MVP & Adobe Community Professional

          It appears you are assuming that an incomplete field have a value of a null string. This might not be case for a number of fields. For example a combo list or drop down box in many cases will have a value of a space or check boxes or radio buttons will have a value of "Off" when no specific check box or radio button is selected. Some developers even use a default text string to display the desired format of the data to be entered.

           

          Fields have a value and a default value. I would look at comparing the value of the field to the default value of the field.

          • 2. Re: Javascript - Fillable form in Acrobat
            rmck7984 Level 1

            I am not a developer, so you may have to break it down a bit.  I have pieced together the script that I included in the original post.  I went back and changed that to off.  Now I it will see the Radio buttons, however, not matter what is selected, it pops up as if it isn't selected.  So I added a 3rd button (hidden) in the series and labeled it Off and set it as default.  However, when I test and try to select one of the other  options aside from Off, it still populates as if it isn't selected.  This is what the script looks like now.  How can I change this to make it see the radio buttons properly?

             

            var emptyFields = [];
            for (var i=0; i<this.numFields; i++) {
                 var f= this.getField(this.getNthFieldName(i));
                 if (f.type!="button" && f.required ) {
                      if ((f.type=="text" && f.value=="") || (f.type=="radio" && f.value=="off"))
              

            f.strokeColor = color.red;   //Highlights the required fields in red

            emptyFields.push(f.name);
                }
            }

            if (emptyFields.length>0) {
                 app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));
            }
            else app.execMenuItem("Print");

            • 3. Re: Javascript - Fillable form in Acrobat
              Joel_Geraci Adobe Community Professional

              The value of an unchecked check box or radio button is "Off"... "not "off"... line 5 of your code.

              • 4. Re: Javascript - Fillable form in Acrobat
                rmck7984 Level 1

                This didn't make a difference.  It still just sees all of the radio buttons as not being selected, even if I do chose an answer.

                • 5. Re: Javascript - Fillable form in Acrobat
                  Karl Heinz Kremer Adobe Community Professional

                  There are two more problems in your code: A radio button group has the type "radiobutton", and not just "radio", and, you will have to add a block around the two lines that add the field to the emptyFields array:

                   

                  {
                      f.strokeColor = color.red;  //Highlights the required fields in red
                      emptyFields.push(f.name);
                  }
                  
                  • 6. Re: Javascript - Fillable form in Acrobat
                    rmck7984 Level 1

                    This worked like a charm.  Just one more thing that I need assistance with.  There is a couple list fields that I need for that script to include.  I would assume I could just add || after the radio button portion and the tidbit to include the list field, but not sure on the script for those specific type of fields.  Could you help me with adding that part?

                    • 7. Re: Javascript - Fillable form in Acrobat
                      Karl Heinz Kremer Adobe Community Professional

                      That is correct, you can just add another expression to the comparison - the problem is to determine when your list box or dropdown is not filled in correctly. You could for example have a default setting that includes only one space (" ") - you can then test for that:

                       

                                if ((f.type=="text" && f.value=="") || (f.type=="radio button" && f.value=="Off") || (f.type="combobox" == " ") { ...
                      • 8. Re: Javascript - Fillable form in Acrobat
                        rmck7984 Level 1

                        Awesome.  I am that much closer.  That worked to get those fields to appear on the missing information pop up when nothing is selected, but I think it needs a little more due to how I have these setup.  On each of the combo boxes, I set a customization where it makes a text field appear when "Other" is selected.  If "Other" is not selected, then that text field stays hidden.  If "Other" is selected, I need for that text field to be required however, not required if "Other" is not selected.  As of right now, it is showing up as required no matter what they select.  I am not sure if I need to add a little to the script on the combo box, or if I need to add something to the text field itself.

                         

                        As of right now, under the Validate tab on the combo box, I have this showing under the run custom validation script:

                         

                        if (event.value == "Other") {

                            this.getField("Other - Submitted On Explanation").display = display.visible;   

                        }

                        else {

                            this.getField("Other - Submitted On Explanation").display = display.hidden;   

                        }

                         

                        Is there something I can add here to tell it to mark it required if visible, and not required if hidden?  If I mark the text field required, it shows up whether it is visible or hidden, so I know I need something somewhere so that the form knows that exception, just not sure what to put.

                        • 9. Re: Javascript - Fillable form in Acrobat
                          try67 MVP & Adobe Community Professional

                          You can use this code to do it:

                           

                          var f = this.getField("Other - Submitted On Explanation");
                          if (event.value == "Other") {
                              f.display = display.visible; 
                              f.required = true;
                          } else {
                              f.display = display.hidden; 
                              f.required = false;
                          }
                          
                          • 10. Re: Javascript - Fillable form in Acrobat
                            Karl Heinz Kremer Adobe Community Professional

                            At the same time you show/hide the field, you can also set/reset the required 'Field.property' of the field. See here for information about the property in the API documentation: Acrobat DC SDK Documentation

                             

                            var f = this.getField("Other - Submitted On Explanation");
                            if (event.value == "Other") {
                                f.display = display.visible; 
                                f.required = true;
                            }
                            else {
                                f.display = display.hidden; 
                                f.required = false;
                            }
                            
                            • 11. Re: Javascript - Fillable form in Acrobat
                              rmck7984 Level 1

                              Thank you, you have been a huge help through this whole process.  It is much appreciated!!!!