Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating button to check required fields

Community Beginner ,
Mar 02, 2022 Mar 02, 2022

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!

TOPICS
JavaScript , PDF forms
6.2K
Translate
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
3 ACCEPTED SOLUTIONS
Community Expert ,
Mar 02, 2022 Mar 02, 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.");}


PDF Acrobatic, InDesigner & Photoshoptographer

View solution in original post

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Have you changed the script?

View solution in original post

Translate
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
Community Expert ,
Mar 03, 2022 Mar 03, 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.

View solution in original post

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Have you saved, closed, and reopened the form?

Translate
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
Community Beginner ,
Mar 02, 2022 Mar 02, 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.

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Can you share the form?

Translate
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
Community Beginner ,
Mar 02, 2022 Mar 02, 2022
 
Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Check the Javascript console for errors.

Translate
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
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

I received this error message:

 

TypeError: f is null
7:AcroForm:Click to Check for Required Fields:Annot1:MouseUp:Action1

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Test also f != null

 

Translate
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
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

No change 

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 2022

Have you changed the script?

Translate
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
Community Beginner ,
Mar 03, 2022 Mar 03, 2022

I must have had some other error, because that code seemed to fix it. Thank you so much!!

Translate
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
Community Expert ,
Mar 02, 2022 Mar 02, 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.");}


PDF Acrobatic, InDesigner & Photoshoptographer
Translate
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
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

New error message:

TypeError: f is null
5:Field:Mouse Up

Translate
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
Community Expert ,
Mar 03, 2022 Mar 03, 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.

Translate
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
Community Expert ,
Mar 03, 2022 Mar 03, 2022

Oops!

I edited my post.


PDF Acrobatic, InDesigner & Photoshoptographer
Translate
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
Community Beginner ,
Mar 03, 2022 Mar 03, 2022

Thank you both! I've got it working now. I appreciate your help!

Translate
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 ,
Aug 04, 2022 Aug 04, 2022

I know I'm late to the game on this one, but I have a question. I have inserted this code on the save button action, because we don't use the submit form action. Is there a way to cancel the save as action when the "ok" button is clicked on the alert with the list of fields not completed? I'm guessing there needs to be more if-else scripting added? I'm just not sure how to do it.

I have like three javascript actions combined with the save as execute menu item action. If I could help combining all three into one javascript action, that would be very helpful. I'm still learning, so forgive my ignorance.

Translate
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
Community Expert ,
Aug 04, 2022 Aug 04, 2022

> Is there a way to cancel the save as action when the "ok" button is clicked on the alert with the list of fields not completed?

Yes, but then it all has to be done with a single script. Something like this:

 

var isFilled = ...;

if (isFilled) app.execMenuItem("SaveAs");

else app.alert("You did not fill on all the fields.");

Translate
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 ,
Aug 04, 2022 Aug 04, 2022

SyntaxError: syntax error
1: Line2

Translate
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
Community Expert ,
Aug 05, 2022 Aug 05, 2022

That was of course not the full code... You need to fill in the logic to check if all the fields are filled in and assign the result as a boolean value to isFilled. I omitted that part, assuming you already have it.

Translate
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 ,
Jun 21, 2023 Jun 21, 2023

Hello, please could you advise if this script could be adjusted so that when all required fields have been filled in then it does the save as function, but it won't do the save as until all required fields have been done? 

Thanks

Dale

Translate
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
Community Expert ,
Jun 21, 2023 Jun 21, 2023

This was answered above. You can't prevent saving altogether, if that's what you mean. You can force the saved file to be blank, though, but that requires a more complex script, like this (paid-for) tool I've developed: https://www.try67.com/tool/acrobat-validate-required-fields-before-printing-or-saving

 

Translate
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 ,
Nov 03, 2023 Nov 03, 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

Translate
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
Community Expert ,
Nov 03, 2023 Nov 03, 2023
LATEST

Post your code, please.

Translate
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