Skip to main content
Participating Frequently
March 2, 2022
Answered

Creating button to check required fields

  • March 2, 2022
  • 4 replies
  • 7982 views

Hi!

I'm working on a form with lots of required fields. After doing some research, I've found the best way to have clients check for required fields (since they won't be submitting the document) is to have a button that says" "You missed some fields: *and pushes all unfilled and required field names*" or a message saying 'Hooray, you filled out all required fields."

 

Here is what I have so far in the actions, mouse up, run javascript:

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! You must fill in the following fields:\n" + emptyFields.join("\n"));

}
if (!(emptyFields.length>0)) {
app.alert("All Required Fields have been answered. Thank you for filling out this form.");
}

When I added the button, it worked great! But after renaming my fields to follow tab order, the button now does not function at all. When you click it nothing happens. Please help!

This topic has been closed for replies.
Correct answer try67

Small error in that code. This part:

f.value == f.defaulValue

should be:

f.value == f.defaultValue

 

I would also add a check whether f is null, like this:

if (f!=null && f.type!="button" && f.required && f.valueAsString == f.defaultValue)

 

Edit: I made a mistake myself... Fixed it now.

4 replies

New Participant
November 3, 2023

I know I am late to the game. I have the same error message, when I run the Java script it says: SyntaxError: 1:Console:Exec undefined?

Help

try67
Community Expert
November 3, 2023

Post your code, please.

JR Boulay
Community Expert
March 2, 2022

Try this script:

 

var emptyFields = [];

for (var i=0; i<this.numFields; i++) {
var f= this.getField(this.getNthFieldName(i));
if (f.type!="button" && f.required && f.value == f.defaultValue) {emptyFields.push(f.name);}
}

if (emptyFields.length>0) {app.alert("Error! You must fill in the following fields:\n" + emptyFields.join("\n"));}
else {app.alert("All Required Fields have been answered. Thank you for filling out this form.");}

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
March 2, 2022

New error message:

TypeError: f is null
5:Field:Mouse Up

try67
try67Correct answer
Community Expert
March 3, 2022

Small error in that code. This part:

f.value == f.defaulValue

should be:

f.value == f.defaultValue

 

I would also add a check whether f is null, like this:

if (f!=null && f.type!="button" && f.required && f.valueAsString == f.defaultValue)

 

Edit: I made a mistake myself... Fixed it now.

Bernd Alheit
Community Expert
March 2, 2022

Can you share the form?

Participating Frequently
March 2, 2022
WIP_SD_app.pdf
Bernd Alheit
Community Expert
March 2, 2022

Check the Javascript console for errors.

Bernd Alheit
Community Expert
March 2, 2022

Have you saved, closed, and reopened the form?

Participating Frequently
March 2, 2022

Yes, but to no avail. I even tried making a fillable copy where the PDF was not editable, but that also didn't work.