Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Validate that one or more checkbox are ticked.

Avatar

Level 2

Currently I'm checking if the user has ticked at least one of the checkboxes by using:

if(CheckBox1.rawValue == 0 && CheckBox2.rawValue == 0){
app.alert("Please select one or more checkboxes.");
}

Is there a way to count the number of checkboxes and do a loop. If you have 15 checkboxes the code above gets a bit hard to read.

I've also use the <validate nullTest="error"/> in the xml to flag the checkbox as being required but this is not true. As long as one or more checkboxes are select the user should not be alerted or shown that checkboxes are not selected. Is there a way to highlight the checkboxes?

1 Reply

Avatar

Level 6

This is what I would do.....

1. Set up a Global Variable under File>>Form Properties>>Variables Tab

     //Variable declared here are always considered string

     Example: GCheckCount and set the initial value 0

2. For each CheckBox I would add the following code under "change" event

if (this.rawValue == 1) {

     GCheckCount.value = "" + GCheckCount.value + 1;

}else {

     GCheckCount.value = "" + GCheckCount.value - 1;

}

3. Also for each CheckBox I would add the following code under "docReady" event to get the initail count

if (this.rawValue == 1) {

     GCheckCount.value = GCheckCount.value + 1;

}

4. When you validate do the following

if (GCheckCount.value == "0") {

     app.alert("Please select one or more checkboxes.");

}