1 Reply Latest reply: Apr 14, 2009 11:05 AM by Varma_LC RSS

    Validate that one or more checkbox are ticked.

    John.Kordas Community Member

      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. Re: Validate that one or more checkbox are ticked.
          Varma_LC Community Member

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

          }