Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Have you changed the script?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Have you saved, closed, and reopened the form?
Copy link to clipboard
Copied
Yes, but to no avail. I even tried making a fillable copy where the PDF was not editable, but that also didn't work.
Copy link to clipboard
Copied
Can you share the form?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Check the Javascript console for errors.
Copy link to clipboard
Copied
I received this error message:
TypeError: f is null
7:AcroForm:Click to Check for Required Fields:Annot1:MouseUp:Action1
Copy link to clipboard
Copied
Test also f != null
Copy link to clipboard
Copied
No change
Copy link to clipboard
Copied
Have you changed the script?
Copy link to clipboard
Copied
I must have had some other error, because that code seemed to fix it. Thank you so much!!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
New error message:
TypeError: f is null
5:Field:Mouse Up
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Oops!
I edited my post.
PDF Acrobatic, InDesigner & Photoshoptographer
Copy link to clipboard
Copied
Thank you both! I've got it working now. I appreciate your help!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
> 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.");
Copy link to clipboard
Copied
SyntaxError: syntax error
1: Line2
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Post your code, please.

