Highlighted

Checkboxes - Require at least one is checked

Avatar

16-03-2009

I have 4 checkboxes where the user can select any, several or all of them, but I want to require that at least one is checked. Is this posible?



They are named as follows:



Checkbox 1: Lateral

Checkbox 2: Main

Checkbox 3: Inlet

Checkbox 4: Other

Replies

Avatar

Avatar

Steve_L_Walker

Total Posts

1.4K

Likes

197

Correct Answer

212

Avatar

Steve_L_Walker

Total Posts

1.4K

Likes

197

Correct Answer

212
Steve_L_Walker

16-03-2009

Kevin,



Given a form with the root 'form1', a subform called 'subform1', and four checkboxes called 'lateral', 'main', 'inlet', and 'other', you could run a script on the change event of each checkbox to compare the current checkbox with all the others. For example,



form1.subform1.lateral::change - (JavaScript, client)



if (form1.subform1.lateral.rawValue == "0") {

if (form1.subform1.main.rawValue == "0" && form1.subform1.inlet.rawValue == "0" && form1.subform1.other.rawValue == "0") {

// xfa.host.messageBox("Please select a minimum of one option.","Options",1);

form1.subform1.other.rawValue = "1";

}

}



form1.subform1.main::change - (JavaScript, client)



if (form1.subform1.main.rawValue == "0") {

if (form1.subform1.lateral.rawValue == "0" && form1.subform1.inlet.rawValue == "0" && form1.subform1.other.rawValue == "0") {

// xfa.host.messageBox("Please select a minimum of one option.","Options",1);

form1.subform1.other.rawValue = "1";

}

}



form1.subform1.inlet::change - (JavaScript, client)



if (form1.subform1.inlet.rawValue == "0") {

if (form1.subform1.lateral.rawValue == "0" && form1.subform1.main.rawValue == "0" && form1.subform1.other.rawValue == "0") {

// xfa.host.messageBox("Please select a minimum of one option.","Options",1);

form1.subform1.other.rawValue = "1";

}

}



form1.subform1.other::change - (JavaScript, client)



if (form1.subform1.other.rawValue == "0") {

if (form1.subform1.lateral.rawValue == "0" && form1.subform1.main.rawValue == "0" && form1.subform1.inlet.rawValue == "0") {

// xfa.host.messageBox("Please select a minimum of one option.","Options",1);

form1.subform1.other.rawValue = "1";

}

}



This will force 'other' to be selected. Of course, if a user doesn't click any checkbox than you will have to put additional logic to validate prior to printing, saving, submitting, or whatever action signifies form fill completion.



Steve
Highlighted

Avatar

17-03-2009

I'd ask Paul Guerette for one of his fake e-mail button forms.



There in the click event, of the fake-mail-button, java script, put the following:



if (form1.subform1.lateral.rawValue == "0" && form1.subform1.other.rawValue == "0" && form1.subform1.main.rawValue == "0" && form1.subform1.inlet.rawValue == "0")

{xfa.host.messageBox("Please select at least one checkbox.");}



The user shouldn't be able to send it, without less than one checkbox activated. Of course you can set ur errormessage as you please and add more scripts f.ex. to make the boarders of the checkboxes red etc..
Highlighted

Avatar

Avatar

pguerett

Total Posts

7.7K

Likes

611

Correct Answer

692

Avatar

pguerett

Total Posts

7.7K

Likes

611

Correct Answer

692
pguerett

17-03-2009

Both of the mentioned methods will work but there is a better way. One of my colleages writes a blog about XFA and forms. Here is the URL:



http://blogs.adobe.com/formfeed



This is an excellent blog and deals with advanced topics (sometimes too advanced). Anyways in the topic of validations he wrote one about exclusion groups ...I suggest that you read that one ....he has a sample in there that does exactly what you are asking.
Highlighted

Avatar

17-03-2009

Thanks everyone for the input and assistance. I ended up using Lisa's method and added a xfa.event.cancelAction.



It seems to work well.



if (topmostSubform.Page1.Lateral.rawValue == "0" && topmostSubform.Page1.Main.rawValue == "0" && topmostSubform.Page1.DrainInlet.rawValue == "0" && topmostSubform.Page1.Other.rawValue == "0")

{xfa.host.messageBox("Please select at least one checkbox for Type."); xfa.event.cancelAction = 1;}