Script for a button to check that required fields are completed (including radio buttons)
Hello. I have a form which includes text fields (ID number, date of birth), quite a few radio buttons for numerous questionnaires (some are yes, no, or unsure questions while others are numerical scales ), and some dropdown lists, almost all of which are required fields. I have a button at the end of the document which auto saves the form for me.
I would also like this button to check if all the required fields are completed, and report back any that aren't in an alert. I have found the following Javascript code (from here Re: Check to see if any field is empty) which partly works.
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("Please complete the following fields:\n" + emptyFields.join("\n"));
}
It reports back accurately on the text fields, however it doesn't work for the radio buttons or dropdowns. I tried adding the script || (f.type=="radios" && f.value=="Off") after the checkbox part, which resulted in all the radio buttons being listed in the alert even if they were filled. Perhaps this is because, by the nature of radio buttons, you can only select one option so some buttons are always left unchecked. I'm not particularly familiar with JavaScript so I am having trouble trying to solve it myself. Any help would be appreciated. I'm using Acrobat XI Pro. Thanks.
