10 Replies Latest reply on Mar 5, 2012 2:25 PM by VickiTuch

    pdf forms - help with calculation with alert message

      Acrobat 8 on a mac

      I am new to pdf forms and even newer to javascript. I need help with a calculation that will only allow entries that add up to 100 (%); i.e. the values in "field 1" plus "field 2" must equal 100 and allow for the possibility that no value is entered in "field 2" (100+0=100). I also would like to display a message when values do not add up to 100, alerting them to revise their entries...

      I have managed to locate, copy and paste document and keystroke javascripts to autotab and validate dates (12 months, 31 days, etc) but have not had any success with this specific calculation.

      Thanks
        • 1. Re: pdf forms - help with calculation with alert message
          evil8
          The way I do it is to have a hidden field that totals both "field 1" and "field 2". Then I test that hidden field with an if statement. If not 100% then I use app.alert("Your msg here") followed with a setFocus to return to "field 1".

          if(this.getField("total").value !== 100) {
          app.alert("Your percentage totals do not add up to 100%");
          }
          this.getField("field 1").setFocus();

          I had to simplify this example, because I do other checks attached to a button, but this should at least get you started in the right direction.

          Hope this helps
          Gene
          • 2. Re: pdf forms - help with calculation with alert message
            gkaiseril MVP & Adobe Community Professional
            One has to be careful because an empty numeric field is treated as zero, so one needs to check if both fields have a value or one is equal to 100.

            event.value = ''; // clear field
            // get the values of field 1 and 2 as string
            var sField1 = this.getField("field 1").value.toString();
            var sField2 = this.getField("field 2").value.toString();
            // adjust for a field being 100 and the other is null
            if (sField1 == 100 & sField2 == '') sField2 = "0";
            if (sField2 == 100 & sField1 == '') sField1 = "0";

            // do only if field 1 and 2 are not null
            if(sField1 != '' & sField2 != '') {
            event.value = Number(sField1) + Number(sField2);
            if (event.value != 100) app.alert("Fields 1 and 2 must sum to 100!", 1, 0);
            } // end not empty

            You may also need to provide a check for a print or submit action to make sure the total field for fields 1 and 2 equals 100.
            • 3. Re: pdf forms - help with calculation with alert message
              Level 1
              Thanks for your insight. I am new to javascript and have an embarrassing question. Is this a document javascript or a custom keystroke script? or do I need a combination of the two?
              • 4. Re: pdf forms - help with calculation with alert message
                rwesterpchef

                Similar circumstance, but 3 potential fields.  If Field1 (Primary Beneficiary) is 100%, then the user will leave Fields 2 and 3 empty.  If Fields 1 and 2 equal 100%, then Field 3 will be empty.  Otherwise they will have values in Fields 1, 2, and 3.  Regardless, they should not be able to continue completing the rest of the form until the sum of these three fields equal 100%.  Therefore I did modify the form as follows, but two issues: it throws the error message up even when first entering a value in Field 1.  This script need not run until all 3 fields have been tabbed through.  The "Total 100" (name of this particular field) is in the Tab Order to appear after "Beneficiary 3 Benefit Percent" field.  And secondly, it still permits the user to continue rather than returning to Field1.  Your assistance is greatly appreciated!  PS:  How do I mark that the post was either helpful or correct?

                Rita

                 

                event.value = ''; // clear field

                 

                // get the values of Beneficiary Benefit fields 1, 2 and 3 as string
                var sField1 = this.getField("Beneficiary 1 Benefit Percent").value.toString();
                var sField2 = this.getField("Beneficiary 2 Benefit Percent").value.toString();
                var sField3 = this.getField("Beneficiary 3 Benefit Percent").value.toString();

                 

                // adjust for a field being 100 and others null
                if (sField1 == 100 & sField2 == '' & sField3 == '') sField2 = "0" , sField3 = "0";
                if (sField2 == 100 & sField1 == '' & sField3 == '') sField3 = "0" , sField1 = "0";

                 

                // do only if Beneficiary Benefit fields 1, 2 and 3 are not null
                if(sField1 != '' & sField2 != '' & sField3 != '') {
                event.value = Number(sField1) + Number(sField2)+ Number(sField3);
                if (event.value != 100) app.alert("The sum of Benefit percent must equal 100.", 1, 0);
                }

                this.getField("field 1").setFocus();

                • 5. Re: pdf forms - help with calculation with alert message
                  VickiTuch Level 1

                  Dear GKaiseril,

                   

                  Thank you for your code which I have implemented successfully:

                   

                  event.value = ''; // clear field

                  // get the values of field 1 and 2 as string

                  var sField1 = this.getField("field 1").value.toString();

                  var sField2 = this.getField("field 2").value.toString();

                  // adjust for a field being 100 and the other is null

                  if (sField1 == 100 & sField2 == '') sField2 = "0";

                  if (sField2 == 100 & sField1 == '') sField1 = "0";

                   

                  // do only if field 1 and 2 are not null

                  if(sField1 != '' & sField2 != '') {

                  event.value = Number(sField1) + Number(sField2);

                  if (event.value != 100) app.alert("Fields 1 and 2 must sum to 100!", 1, 0);

                  } // end not empty

                   

                  Just wondered if you can let me know what I need to add to stop the form being submitted(email submit button) if the amount is not 100?

                  So the same alert appears ("Fields 1 and 2 must sum to 100!")

                   

                  Many thanks

                  • 6. Re: pdf forms - help with calculation with alert message
                    gkaiseril MVP & Adobe Community Professional

                    It is custom calculation script.

                    • 7. Re: pdf forms - help with calculation with alert message
                      VickiTuch Level 1

                      Hi GKaiseril

                       

                      Or is there a way so that the alert will keep appearing until correct values are added to make total 100?

                       

                      Currently if the fields exceed or are below 100, then the alert message  ("Fields 1 and 2 must sum to 100!")

                      appears once and you can still tab to next field, or click on next field or submit the pdf.

                       

                      Thanks

                      • 8. Re: pdf forms - help with calculation with alert message
                        gkaiseril MVP & Adobe Community Professional

                        And if one is locked to the last field, how would one get back to a prior field where the error could be without entering the wrong data into the current field.

                         

                        That is why I had a comment about needing additional checking of the data prior to printing and saving.

                        • 9. Re: pdf forms - help with calculation with alert message
                          VickiTuch Level 1

                          I was hoping it be be similar to required fields where you are alerted if not filled in and cannot submit until you imputted the correct calculation to be 100?

                          • 10. Re: pdf forms - help with calculation with alert message
                            VickiTuch Level 1

                            Trying out the script, it only shows the alert only when you click or tab into the total field (where Ive added the below code),

                            is there a way that if you did not click in the total field and the amount didnt equal 100

                            the alert would still appear? This is the current code used.

                             

                            event.value = ''; // clear field

                            // get the values of field 1 and 2 as string

                            var sField1 = this.getField("field 1").value.toString();

                            var sField2 = this.getField("field 2").value.toString();

                            // adjust for a field being 100 and the other is null

                            if (sField1 == 100 & sField2 == '') sField2 = "0";

                            if (sField2 == 100 & sField1 == '') sField1 = "0";

                             

                            // do only if field 1 and 2 are not null

                            if(sField1 != '' & sField2 != '') {

                            event.value = Number(sField1) + Number(sField2);

                            if (event.value != 100) app.alert("Fields 1 and 2 must sum to 100!", 1, 0);

                            } // end not empty