Skip to main content
This topic is closed to new replies. Start a new post to keep the conversation going.
George_JohnsonCorrect answer
Inspiring
September 30, 2016

I expected you would change the code based on what I showed. So change that one line to:

          if ((f.type == "text" && f.valueAsString === "") || (f.type == "checkbox" && f.value === "Off")) emptyFields.push(f.name);

7 replies

associationa93135951
Participant
October 23, 2016

Tks !

Actually if all the mandatory fields are not completed, the script stops. I don't want it to stop until all the fields are completed because when all fields are completed I have a second command allowing to print and save.

All is in form created with acrobat XI.

I don't know how to send you if necessary

Translate
associationa93135951
Participant
October 23, 2016

Hello,

Maybe it's not the correct place, but I use the same script but have a roblem : I want tol 'loop' until all the fields are completed /

What's wrong in my current script ?

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.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

app.alert("Attention !\Vous n'avez pas complété vos références suivantes\n\n" + emptyFields.join("\n"));                   Si la condition n’est pas réunie il faut recommencer jusqu’à ce que tout les champs soient remplis :

}

if (emptyFields.length==0) {

app.alert("Vos références sont remplies, vous pouvez imprimer",3); La j’appelle une autre fonction qui marche

}

Translate
try67
Community Expert
Community Expert
October 23, 2016

What do you mean by "loop until all the fields are completed"? Please clarify.

Translate
Inspiring
September 30, 2016

You have to get the value as a string using the valueAsString field property and compare it to a zero length string (""). For exmaple:

var sVal = getField("Text1").valueAsString;

if (sVal === "") {

    app.alert("The field is empty");

}

This is equivalent:

if (!sVal) {

    app.alert("The field is empty");

}

or similarly:

if (sVal) {

    app.alert("The field is not empty");

}

Translate
September 30, 2016

I'm afraid the code won't work for me as it only tests 1 field. The code I'm using now tests all the fields in the PDF:

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=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

     }

}

if (emptyFields.length>0) {

     app.alert("Error!\nYou must fill in the following fields:\n\n" + emptyFields.join("\n"));

}

if (emptyFields.length==0) {

     app.alert("All fields completed. Thank you!",3);

}

Translate
George_JohnsonCorrect answer
Inspiring
September 30, 2016

I expected you would change the code based on what I showed. So change that one line to:

          if ((f.type == "text" && f.valueAsString === "") || (f.type == "checkbox" && f.value === "Off")) emptyFields.push(f.name);

Translate