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

New Here ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

503

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Dec 14, 2016 Dec 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...

Likes

Translate

Translate
Adobe Community Professional ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

LATEST

Thank You.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines