Skip to main content
Sep 29, 2016 11:12 PM UTC
Answered

How do I check if a field is empty? The code I have thinks a "0" is empty, which is false.

The code I used is copied from this thread: https://forums.adobe.com/thread/1223607

I created a button that will list out each field if it is left blank. However, a field will still pop up even though there is a character in it, namely "0".

Please help.

This topic is closed to new replies. Start a new post to keep the conversation going.
George_JohnsonCorrect answer
Inspiring
Sep 30, 2016 6:34 PM UTC

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
Oct 23, 2016 7:49 AM UTC

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
Oct 23, 2016 6:14 AM UTC

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
Oct 23, 2016 7:20 AM UTC

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

Translate
Inspiring
Sep 30, 2016 5:01 AM UTC

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
Sep 30, 2016 3:20 PM UTC

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
Sep 30, 2016 6:34 PM UTC

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