Expand my Community achievements bar.

Help writing JavaScript using an array to require at least one checkbox is selected prior to Save

Avatar

Level 1

I have a group of 29 checkboxes that I need to require at least one be checked prior to someone saving the form. (The validation taking place prior to Saving is critical.) Since there are 29 checkboxes in the group, I’d like to use an array rather than individually addressing each box. Unfortunately this has proved to be beyond my scripting abilities.

Below is the script that I’ve been trying to use/adapt. It doesn’t seem to do anything, and I’m not even sure on which event I should be placing it.

var checkboxVal = false;    

for (var i=0; i<WCForm.InjuredBodyPart.length; i++)

{   

  if (WCForm.InjuredBodyPart[i].checked) checkboxVal = true;    

}    

if (!checkboxVal)

{    

  alert("Please select at least one Injured Body Part.");    

  return false;

} 

6 Replies

Avatar

Level 10

Hi,

If all of the checkboxes are on the one page AND they have the same name, then you could add the values of the checkboxes using FormCalc.

if (Sum(page1.CheckBox1[*]) ge 1) then

     // all is okay, at least one checkbox is ticked

else

     app.alert("Please select at least one Injured Body Part."); 

endif

If your users have Acrobat/Reader v9.1 or above, then you could set the target version to this AND use the cancelAction method (see help). You could put this line above the app.alert.

The whole script block could then go in the preSave event.

Here is a similar example: http://assure.ly/omsURE.

Hope that helps,

Niall

Avatar

Level 1

Hi,

Thank you for the quick response. Unfortunately, something isn't going right. Below is the script I placed on the preSave event for the SubForm in which I've wrapped the checkboxes. (I apologize; I failed to mention the SubForm in my inital post.) I changed page1 to P3 as the group of checkboxes is on page 3 and I had renamed it to P3.

if

(Sum(P3.InjuredBodyPart[*]) ge 1) then

    

// all is okay, at least one checkbox is ticked

else

     app.alert("Please select at least one Injured Body Part.")

; 

endif

Here is the error message I get prior to saving regardless of whether a box is check.

8-24-2011 8-20-22 AM.jpg

Avatar

Level 10

Hi,

My bad. When using FormCalc please use xfa.host.messageBox() to display the message.

See Help for the four parameters as to how you want the message to display.

Niall

Avatar

Former Community Member

Hi Niall,

I've tried to incorporate your example into my form which has a few groups of checkboxes that require at least a single selection. I've got it to work but not without adding in an action for when all is OK.

If there is one selected, I'd like the validation to just move on to the next question.

I had to add a messagebox basically saying validation is OK for the script not to produce an error.

I have the checkboxes wrapped in a subform and all are the same name. Below is the script I currently have in the exit event of the subform. When the line "xfa.host.messageBox("all is ok");" is  commented out, I get an error. If it is not commented out, the script works but I would rather not have to have the OK messagebox. I'm not sure how to get it to "do nothing" when at least one item it already checked when the script runs. 

if (Sum(SourceOfWealth.SourceWlth[*]) ge 1) then

       // all is okay, at least one checkbox is ticked

   // xfa.host.messageBox("all is ok");  

else

xfa.host.messageBox("Please select at least one Source of Wealth checkbox for Question 12");

xfa.host.setFocus("PersonalEDDQ.PBPernsEDDQ.EDDQSectionI.SourceOfWealth.SourceWlth[0]");

endif

Any additional help will be greatly appreciated! Thanks!


Avatar

Level 7

Can you just change the logic of the if statement?

Ex:


if(Sum(SourceOfWealth.SourceWlth[*]) lt 1) then


  xfa.host.messageBox("Please select at least one Source of Wealth checkbox for Question 12");


  xfa.host.setFocus("PersonalEDDQ.PBPernsEDDQ.EDDQSectionI.SourceOfWealth.SourceWlth[0]");


endif


Avatar

Former Community Member

Well that makes perfect sense. Real "Duh" moment for me.

Worked just fine...Thanks!