Copy link to clipboard
Copied
I have a form with several drop downs and many text fields built in the latest version of Acrobat DC.
I have added a submit button that tests for required fields before submission.
It tests text fields OK, but if the drop down has ANY value including the default "Please select" it considers that as a valid answer, despite a script added to ensure the drop down is always set as a required field if the value = "Please select".
I have added this script to the custom format section, and tried it in Actions, Validation and Calculate sections, but it always has the same result. The submit button ignores that the drop down setting as "required".
I have scoured the web for hours, there are many suggestions but look a bit complex and beyond my novice JavaScript skills.
How do I get the submit button to check if a drop down that has the default value, and to treat it as an empty field like a text field?
Appreciate any advice.
Thanks try67.
I added this script at the document level and called the function from the Mouse Up Action of the button I created (rather than the standard Submit button) and it worked on a test form. The JavaScript warning message lists the fields that require filling in.
I have no doubt it will work on my larger form.
Appreciate your assistance.
Copy link to clipboard
Copied
This issue was discussed here many times. I've provided code that all validate all required fields, including drop-downs.
Try searching for "validate required fields" and you're sure to find it.
Copy link to clipboard
Copied
Thanks try67.
I have searched the forum under this topic, as well as filtering by your user name, but which one of the myriad of posts is the one that you provide clarity on checking multiple fields for valid data?
I have tried this script with one drop down and it works fine (just need to add specifics about email parameters).
var f = this.getField("VehicleActivities");
if (f.valueAsString==f.defaultValue) {
app.alert("You must fill in this field before submitting the file.",1);
f.setFocus();
} else this.mailDoc();
How can I test another dozen or more fields?
Cheers.
Copy link to clipboard
Copied
If you can’t extend that code to check multiple values you need to study JavaScript at a very basic level, because your question is really “How do I test multiple conditions“. There are multiple ways including nested if statements, and logical operators like &&.
Copy link to clipboard
Copied
Here's a generic function I wrote that does it. It returns true if the validation of all the required fields was successful, or false otherwise:
function validateRequiredFields(doc) {
var emptyFields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if (f!=null && f.type!="button" && f.required && f.valueAsString==f.defaultValue) {
emptyFields.push(f.name);
}
}
if (emptyFields.length>0) {
app.alert("You must fill in the following fields:\n" + emptyFields.join("\n"));
return false;
}
return true;
}
So you can use it like this:
if (validateRequiredFields(this)) this.mailDoc();
Copy link to clipboard
Copied
Thanks try67.
I added this script at the document level and called the function from the Mouse Up Action of the button I created (rather than the standard Submit button) and it worked on a test form. The JavaScript warning message lists the fields that require filling in.
I have no doubt it will work on my larger form.
Appreciate your assistance.
Copy link to clipboard
Copied
Form field objects have many properties. They have a value and a defaultValue. The first is the value of the field. The latter is the stafting or value of the field whem the form is field is reset yo its initial value.