Skip to main content
sashad29145705
Participant
August 16, 2017
Answered

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

  • August 16, 2017
  • 3 replies
  • 12076 views

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.

This topic has been closed for replies.
Correct answer try67

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);

3 replies

JR Boulay
Community Expert
Community Expert
December 10, 2021

"yes it would be better if able show all errors at once"

You are going to drive your users crazy!

 

I use to turn all empty required fields with a red stroke, and before I added this script in action "onFocus" to all of them:

event.target.strokeColor = color.transparent;

(transparent or the previous color if any)

Acrobate du PDF, InDesigner et Photoshopographe
Participant
January 18, 2019

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

try67
Community Expert
Community Expert
January 18, 2019

Add this as the last line of code:

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

Participant
May 24, 2021

I want to Show some message is ther is no empty fields. I tried adding 

else app.alert("No errors found.",3); but shows no errors found even there is empty field.

can you help please

 

var requiredFields = new Array(24);
requiredFields[0] = "Aadhaar Number";
requiredFields[1] = "AplVal";
requiredFields[2] = "Applicant Name Eng";
requiredFields[3] = "Applicant Name Tam";
requiredFields[4] = "GenVal";
requiredFields[5] = "MarVal";
requiredFields[6] = "Date of Birth_af_date";
requiredFields[7] = "RelVal";
requiredFields[8] = "Father Name Eng";
requiredFields[9] = "Father Name Tam";
requiredFields[10] = "Mother Name Eng";
requiredFields[11] = "Mother Name Tam";
requiredFields[12] = "RgnVal";
requiredFields[13] = "ComVal";
requiredFields[14] = "OcuVal";
requiredFields[15] = "DisVal";
requiredFields[16] = "TalVal";
requiredFields[17] = "Street Name Eng";
requiredFields[18] = "Street Name Tam";
requiredFields[19] = "Door Number";
requiredFields[20] = "Pin Code";
requiredFields[21] = "PinVal";
requiredFields[22] = "Mobile Number";
requiredFields[23] = "MobVal";


// These are the alert messages shown when a required field is empty.// Populate array with messages.// Make sure there's one message for each required field.

 

var alertMsg = new Array(24);
alertMsg[0] = "Please enter Aadhaar Number.";
alertMsg[1] = "Please Select Appelation.";
alertMsg[2] = "Please enter Applicant Name in English.";
alertMsg[3] = "Please enter Applicant Name in Tamil.";
alertMsg[4] = "Please Select Gender.";
alertMsg[5] = "Please Select Marital status.";
alertMsg[6] = "Please enter Date of birth.";
alertMsg[7] = "Please Select Relationship.";
alertMsg[8] = "Please enter Father name in English.";
alertMsg[9] = "Please enter Father name in Tamil.";
alertMsg[10] = "Please enter Mother name in English.";
alertMsg[11] = "Please enter Mother name in Tamil.";
alertMsg[12] = "Please Select Religion.";
alertMsg[13] = "Please Select Community.";
alertMsg[14] = "Please Select Occupation.";
alertMsg[15] = "Please Select District.";
alertMsg[16] = "Please Select Taluka.";
alertMsg[17] = "Please enter Street name in English.";
alertMsg[18] = "Please enter Street name in Tamil.";
alertMsg[19] = "Please enter Door Number.";
alertMsg[20] = "Please enter Pincode.";
alertMsg[21] = "Pincode must be 6 digit.";
alertMsg[22] = "Please enter Mobile Number.";
alertMsg[23] = "Mobile Number must be 10 digit.";

var emptyTest = /^\s*$/;
var fieldCount = requiredFields.length
var fld = 0;
for (var i=0; i < fieldCount; i++)
{fld = this.getField(requiredFields[i]);
if( emptyTest.test(fld.value) )
{
app.alert(alertMsg[i]);fld.setFocus();break;
}

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

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 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);

sashad29145705
Participant
August 17, 2017

Thank you, that works perfectly