Skip to main content
February 9, 2017
Answered

Javascript - Fillable form in Acrobat

  • February 9, 2017
  • 1 reply
  • 1858 views

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");

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

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.


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;

}

1 reply

Inspiring
February 9, 2017

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.

February 16, 2017

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");

Joel Geraci
Community Expert
Community Expert
February 16, 2017

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