6 Replies Latest reply on Feb 3, 2011 1:09 PM by sb25newbie

    Looking for help with javascript to autofill checkboxes based on a numeric value.

    sb25newbie

      I have a numeric field that I would like to have 1 - checkbox

      out of five autofilled based on a numeric value.

       

      For instance the numeric value is TotalPoints

       

      If the TotalPoints Value is >10 autofill this checkbox

      If the TotalPoints Value is 10 - 19 autofill this checkbox

      If the TotalPoints Value is 20 - 49 autofill this checkbox

      If the TotalPoints Value is 50 - 69 autofill this checkbox

      If the TotalPoints Value is 70+ autofill this checkbox

       

      Assistance in writing the correct script for this would be greatly appreciated.

        • 1. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
          George_Johnson MVP & Adobe Community Professional

          You can use a custom Validate script for the text field. Here's one way:

           

          // Custom Validate script for text field

          (function () {

           

              //Get this field's value, as a number

              var val = +event.value;

           

              if (val >= 70) {

                  getField("cb5").value = "exportvalue5";

                  return;

              }

           

              if (val >= 50) {

                  getField("cb4").value = "exportvalue4";

                  return;

              }

           

              if (val >= 20) {

                  getField("cb3").value = "exportvalue3";

                  return;

              }

           

              if (val >= 10) {

                  getField("cb2").value = "exportvalue2";

                  return;

              }

           

              // <10

                  getField("cb1").value = "exportvalue1";

           

          })();

           

          Replace "cb1" - "cb5" with the actual names of your check boxes, and "exportvalue1" - "exportvalue5" with their export values that you set up.

          • 2. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
            sb25newbie Level 1

            Thanks for the reply George.  As I said, I'm a newbie to Adobe Acrobat and I must have input something wrong as this did not work.  I did put the validation script in the numeric text field, correct?

            • 3. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
              George_Johnson MVP & Adobe Community Professional

              Do the five check boxes all have the same name, or do they have different names? Can you post a sample form somewhere?

              • 4. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
                sb25newbie Level 1

                All the checkboxes have separate names. 

                 

                Check box #1 is AssetClass.0 with the export value of 1

                Check box #2 is AssetClass.1 with the export value of 2

                Check box #3 is AssetClass.2 with the export value of 3

                Check box #4 is AssetClass.3 with the export value of 4

                Check box #5 is AssetClass.4 with the export value of 5

                 

                Score and Strategy

                Use the following calculation to determine your point score and indentify the appropriate strategy listed below.

                A. Add your points for questions 1 – 2.

                B. Add your points for questions 3 – 12.

                C. Subtract B from A. (Numeric Text Box)  name is TotalPoints

                 

                Points Strategy Asset Class Mix (check boxes as named above)

                 

                0 – 10 Primarily Fixed Income: 80% Fixed Income; 20% Equity

                10 – 19 Balanced Fixed Income-Oriented: 60% Fixed Income; 40% Equity

                20 – 49 Balanced Equity-Oriented: 40% Fixed Income; 60% Equity

                50 – 69 Primarily Equity: 20% Fixed Income; 80% Equity

                70+ Equity: 95%; 5% Cash

                 

                • 5. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
                  George_Johnson MVP & Adobe Community Professional

                  The following script works for me. Note the addition of the resetForm statement, which I should have included before.

                   

                  // Custom Validate script for text field
                  (function () {

                   

                      //Get this field's value, as a number
                      var val = +event.value;

                   

                      resetForm(["AssetClass"]);

                   

                      if (val >= 70) {
                          getField("AssetClass.0").value = 1;
                          return;
                      }

                   

                      if (val >= 50) {
                          getField("AssetClass.1").value = 2;
                          return;
                      }

                   

                      if (val >= 20) {
                          getField("AssetClass.2").value = 3;
                          return;
                      }

                   

                      if (val >= 10) {
                          getField("AssetClass.3").value = 4;
                          return;
                      }

                   

                      // <10
                          getField("AssetClass.4").value = 5;

                   

                  })();

                   

                  Edit: Yes, placing the code in the Validate event of the text field is correct.

                  • 6. Re: Looking for help with javascript to autofill checkboxes based on a numeric value.
                    sb25newbie Level 1

                    Thank you so much.  I greatly appreciate your help!  It worked and it is totally awesome.