5 Replies Latest reply on Dec 16, 2013 7:58 AM by Mohammad Irfan

    How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?

    RWaz

      How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?

        • 1. Re: How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?
          gkaiseril MVP & Adobe Community Professional

          You need to use custom JavaScript to control the check boxes that can be checked. The same script will need to be run by all of the check boxes and you will need to define how you want to handle the check boxes once the desired number of check boxes has been reached. You will also need to provide for the case where a check box becomes unchecked.

          • 2. Re: How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?
            RWaz Level 1

            Hi GKaiseril,

             

            Thanks for your reply.  That's what I'm attempting to do, but I'm largely trying to "borrow" on-line JavaScript (see below) & modify for my PDF Form, w/ rudimentary knowledge gained while surfing the web.  The Form has 30 checkboxes that I want to limit users to a max. of 4.

             

            So any add'l scripting tips would be most appreciated.

             

            Thanks!

             

            var NewCount = 0

            getField("Check_Box1").value === "Yes"

            {NewCount = NewCount + 1}
            getField("Check_Box2").value === "Yes"

            {NewCount = NewCount + 1}

            getField("Check_Box3").value === "Yes"

            {NewCount = NewCount + 1}

            getField("Check_Box4").value === "Yes"

            {NewCount = NewCount + 1}

            getField("Check_Box5").value === "Yes"

              {NewCount = NewCount + 1}

            if (NewCount == 4)
            {
            app.alert(“Pick Just Four Please”); return false;
            }

            • 3. Re: How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?
              Mohammad Irfan Level 1

              Here is the working solution

               

              Please rename all the checkboxes like chkBox0, chkBox1 ...........chkBox30 (must start with 0)

              Export Value of every checkbox should be "Yes" (Yes is default value also)

               

              Put this Code in Document level Javascript

               

              //----------------------------Document JavaScript Code ----------------------------

               

              var counter = 0;                // Checked counter

               

              //--------Count Checked boxes again when document is closed and re-opened ------------

               

              for (var i =0; i<= 30; i++){

                        if (getField("chkBox" + i).value == "Yes"){

                        counter += 1;

                        }

              }

               

              //-------------Validation Function ---------------------------------------------------

               

              function validateCheckBox(name,value){

                        if (value == "Yes" && counter < 4){

                                  counter += 1;

                        }else if (value == "Off"){

                                  counter -= 1;

                        }else{

                                  getField(name).value = "Off";

                                  app.alert("You can select four checkboxes only");

                        }

              }

               

              //-----------------End of Document Javascript------------------------

               

              Now put this line in MouseUp event of all check boxes

               

              //-------------CheckBox MouseUp event Code-----------------------------

               

              validateCheckBox(event.target.name,event.target.value);

               

              //----------------------------------------------------------------------------------

               

              Hope this will help

               

              Regards,

               

              Message was edited by: Mohammad Irfan

              • 4. Re: How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?
                RWaz Level 1

                Hello Mohammad,

                 

                THANKS so much for the script!!!  It works perfectly!  Have a great week.

                 

                -- RWaz

                • 5. Re: How do you use JavaScript to limit the number of CheckBoxes a user can select on a PDF Form?
                  Mohammad Irfan Level 1

                  Please mark the post as helpful/ Answered

                   

                  Regards,

                  1 person found this helpful