Script for a button to check that required fields are completed (including radio buttons)

New Here ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

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.

TOPICS
PDF forms

Views

1.6K

Likes

translate

Translate

Translate

Report

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

correct answers 1 Correct Answer

Most Valuable Participant , Aug 16, 2017 Aug 16, 2017
Change this line:if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);To:if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);

Likes

translate

Translate

Translate
Most Valuable Participant ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

Change this line:

if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

To:

if (f.valueAsString==f.defaultValue) emptyFields.push(f.name);

Likes

translate

Translate

Translate

Report

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 ,
Aug 16, 2017 Aug 16, 2017

Copy link to clipboard

Copied

Thank you, that works perfectly

Likes

translate

Translate

Translate

Report

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 ,
Mar 11, 2020 Mar 11, 2020

Copy link to clipboard

Copied

I was having the same problem and this worked for me too. Thanks for the help

Likes

translate

Translate

Translate

Report

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 ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

I put this code in the document javascripts to make the submit by email button to run.

function Ex2ValidFields()

{

   var bRtn = false;

   var aErrMsg = [];

   var rgEmpty = /^\s*$/;

   if(rgEmpty.test(this.getField("code No").value))

      aErrMsg.push("CODE NO. IS REQUIRED");

 

   if(aErrMsg.length == 0)

      bRtn = true;

   else

      app.alert("Line Check\nOne or more required fields have not been filled out:\n\n   * " + aErrMsg.join("\n   * "));;

   return bRtn;

}

///////////////////

now i want to merg this code:

 

for (var i=0; i<this.numFields; i++) {

     var f= this.getField(this.getNthFieldName(i));

     if (f.type!="button" && f.required ) {

          if (f.valueAsString==f.defaultValue)aErrMsg.push(f.name);

     }

to the first code to read the radio buttons as a required fields

 

can you help please ?

thank you

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

Do you just want to have a special message for that one field?

Likes

translate

Translate

Translate

Report

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 ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Is there a way to add a line that if no errors are found, the warning box says "No errors found"?

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Add this as the last line of code:

else app.alert("No errors found.",3);

Likes

translate

Translate

Translate

Report

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