Skip to main content
Participant
December 14, 2016
Answered

Want to prevent email/print through form button if required fields are blank (but allow blanks in non-required fields)

  • December 14, 2016
  • 1 reply
  • 1312 views

Hi All,

I understand that technically I cannot completely restrict printing and that's fine. I have a print button on my form (with about 25 fields; some required others not) that I'm looking to use some javascript on that will check to see if all the required fields are filled in before allowing to print (any any type of button action). It's OK to print if all the required fields are filled in but some of the non required fields are blank. I found a good deal of answers from other posts but I think my issue (possibly) is that I have several drop down boxes in my form?

I've pasted what I've found so far after doing some forum searching. My problem is that even when I fill in every single field and drop down in my form, I still get the alert saying I need to fill in required fields (instead of printing). My ultimate goal here is when the button is clicked, it checks to see if only the required fields are filled and if they aren't it returns a message saying fields need to be complete and highlights them in red. If the user then enters the required info (but can leave the non-required fields blank) then the fields turn back to their original color (gray) and the person is allowed to use the button action (in this case print).

Thanks!

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.valueAsString=="") || (f.type=="checkbox" && f.value=="Off"))

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

else f.strokeColor = color.gray; //Highlights the filled in fields with gray outline

emptyFields.push(f.name);

}

}

if (emptyFields.length>0) {

     app.alert("WAIT! You must fill in all of the required fields before printing this form:\n");

}

else this.print();

This topic has been closed for replies.
Correct answer try67

You copied or edited parts of the code (which I wrote) incorrectly, and it's also an older version that can be improved. Try this version:

var emptyFields = [];

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

    var f = this.getField(this.getNthFieldName(i));

    if (f==null || f.type=="button") continue;

    if (f.required && f.valueAsString==f.defaultValue) {

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

        emptyFields.push(f.name);

    } else f.strokeColor = color.gray; //Highlights the filled in fields with gray outline

}

if (emptyFields.length>0) {

    app.alert("WAIT! You must fill in all of the required fields before printing this form");

} else this.print();

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 14, 2016

You copied or edited parts of the code (which I wrote) incorrectly, and it's also an older version that can be improved. Try this version:

var emptyFields = [];

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

    var f = this.getField(this.getNthFieldName(i));

    if (f==null || f.type=="button") continue;

    if (f.required && f.valueAsString==f.defaultValue) {

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

        emptyFields.push(f.name);

    } else f.strokeColor = color.gray; //Highlights the filled in fields with gray outline

}

if (emptyFields.length>0) {

    app.alert("WAIT! You must fill in all of the required fields before printing this form");

} else this.print();

Participant
December 15, 2016

The improved code works perfectly! The form now will now print when all required fields are input (even if non mandatory ones aren't). Thank you!