Copy link to clipboard
Copied
Novice Adobe Acrobat user - so please forgive my ignorance.
I'm encountering an issue with some JavaScript that I've poached from this community forum to validate that all of the fields within my form are complete. Here's the 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.");}
My form includes text fields, dropdown lists, and radio buttons. For a handful of the fields I have default values. After completing a test form, I run the script and the output is a warning that not all of the fields have been completed (the language from the script); however, they happen to be the fields that I've utilized a default value. When I adjust one of the "incomplete" fields to something other than the default value the warning prompt states all required fields are complete (the language from the above script); however, deviating from the default value defeats the purpose of having a default value. I've tried troubleshotting, but alas, here I am.
Is this an issue with the script? Or might it be the form? I'm guessing I'm missing something really obvious.
Copy link to clipboard
Copied
I'm looking at these lines
if (f.type!="button" && f.required && f.value == f.defaultValue)
{emptyFields.push(f.name);}}
and specifically this expression f.value == f.defaultValue
It seems to me this code is designed to do exactly what it is you don't want; that is, it is designed to give an error for any field that equals the default value.
Copy link to clipboard
Copied
>> deviating from the default value defeats the purpose of having a default value
That depends. A default value can be used as a "place-holder" value, meant to be replaced by the actual value entered by the user. But if you're using it as a valid option, then you should not set that field as required, or change the condition from comparing to the default value to an empty string (for text fields), for example.
Copy link to clipboard
Copied
It looks like a line break issue, this script works fine for me:
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.");}