12 Replies Latest reply: Mar 6, 2012 7:16 AM by VickiTuch RSS

    Javascript alert script needed when calculation does not equal 100

    VickiTuch Community Member

      I have created a fillable form and have a total box which I've set to calulate the sum of Field1, Field2, Field3 and Field4

      which works well.

       

      I would like to add a javascript alert if the total amount does not add to exactly 100 to stop the user submitting the form or moving to next question.

       

      Any help would be greatly appreciated

       

      Thank you

        • 1. Re: Javascript alert script needed when calculation does not equal 100
          try67 ACP/MVPs

          I don't think it's a good idea to do this when the user fills in the

          fields. In fact, it will make the form unusable. Imagine that all fields

          are empty and the user fills in the first field with 20 (for example). They

          would then get this message, but of course there's nothing they can do

          about it, except for changing the value to 100... In short, not a good idea.

           

          If you want to add an error message to the submit button in case the sum is

          not 100, that's possible, but only if you submit the form using a script as

          well. The code to do that would be something like this:

           

          if (this.getField("Total").value==100) {

          this.submitForm(); // you'll need to specify the relevant parameters here...

          } else {

          app.alert("Error! The total must be equal to 100.");

           

          • 2. Re: Javascript alert script needed when calculation does not equal 100
            VickiTuch Community Member

            Hi try67,

             

            I have implemented successfully the following:

             

            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!")

             

            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.

             

            I was hoping to add validation on submitto be similar to required fields where you are alerted if not filled in and cannot submit until you imputted the correct calculation sum up to 100?

            • 3. Re: Javascript alert script needed when calculation does not equal 100
              try67 ACP/MVPs

              The code I provided will only submit if the fields Total equals 100. But

              you must not use the built-in "Submit a Form"-command for your button. Just

              a "Execute a JavaScript"-command with the provided code.

              • 4. Re: Javascript alert script needed when calculation does not equal 100
                VickiTuch Community Member

                I cant seem to get this to work,

                 

                Would it be possible to change my alert to an alert with the warning with the exclamation in the yellow triangle eg:

                 

                f = getField(event.target.name)

                if (f.value.length == 0)

                    {

                    f.setFocus()

                    //Optional Message - Comment out the next line to remove

                    app.alert("This field is required. Please enter your Name.")

                    }

                 

                But I dont know how to change my validation to  include this?

                • 5. Re: Javascript alert script needed when calculation does not equal 100
                  try67 ACP/MVPs

                  I'm not sure where you're placing this code... But you don't need to use

                  getField. event.target already returns the field object you want to use.

                   

                  To show the yellow warning icon, change your alert command to this:

                  app.alert("This field is required. Please enter your Name.",1)

                  • 6. Re: Javascript alert script needed when calculation does not equal 100
                    VickiTuch Community Member

                    Thanks for your help but Im still struggling with this.

                     

                    My initial code works well but I have a total field that shows the calculation of the 2 fields, so when the result is more or less than 100 the alert displays but

                    only if you click or tab into the total field.

                     

                    Really I would like the alert to show if you did not click or tab into the total field when the total doesnt amount to 100.

                    • 7. Re: Javascript alert script needed when calculation does not equal 100
                      VickiTuch Community Member

                      Dear try67,

                       

                      Thank you for your help in trying to solve my form validation issue.

                       

                      To help explain the problem please see link to the test form pdf I'm working on:

                       

                      https://acrobat.com/app.html#d=i2pUcJ2sVxUANQ7SC3WWjw

                       

                      The error message works well displaying 'Total amount should equal 100 etc' when this criteria is not met.

                       

                      The problem is that it allows you to submit the form even if the 'Total' field does not equal 100.

                      I need to include validation so that the form cannot be submitted until everything has been correctly input.ie Total box must total 100%

                       

                      I'm an absolute beginner at javascript validation but extremily keen to learn.

                      Any help or advice you may offer would be greatly appreciated, I'm keen to solve this problem.

                       

                      Ps do you think the help text works ok? (question mark in red button) next to first question.

                       

                      Kind regards

                      • 8. Re: Javascript alert script needed when calculation does not equal 100
                        try67 ACP/MVPs

                        It seems like you've used some product to generate your code for the submit

                        button. You will need to incorporate the line of code that checks if Total

                        is 100 into that code.

                         

                        Yes, it looks OK, but next time you post a file, do so without the Reader

                        Rights.

                        • 9. Re: Javascript alert script needed when calculation does not equal 100
                          try67 ACP/MVPs

                          Maybe I spoke too soon... The way you set it up now, the error message appears (when the fields do not equal 100) each time you change one of the other fields as well (like pressing one of the radio buttons), which is a bit odd.

                          • 10. Re: Javascript alert script needed when calculation does not equal 100
                            VickiTuch Community Member

                            Thanks for checking the file.

                             

                            Please see new link with standard acrobat button for the submit button and added:

                             

                            https://acrobat.com/app.html#d=URDvjEF4Dghqf4WYwYVQgg

                             

                            I dont know why the alert keeps appearing when you go to the next radio button filed/s?

                             

                            Please can you advise on how I would implement the line of code that checks if Total

                            is 100 on submit.

                             

                            Help appreciated.

                            • 11. Re: Javascript alert script needed when calculation does not equal 100
                              try67 ACP/MVPs

                              The warning message keeps appearing because you've placed it as a part of a

                              calculation script, which executes each time something changes in *any *of

                              the form fields. It's not necessarily bad, since the used will be reminded

                              of the problem until it's fixed.

                               

                              In the code of the button, just before the submitForm command, add the code

                              I've provided before:

                               

                              if (this.getField("Total").value==100) {

                              // place here the submitForm call that you currently have in your file

                              } else {

                              app.alert("Error! The total must be equal to 100.");

                              }

                              • 12. Re: Javascript alert script needed when calculation does not equal 100
                                VickiTuch Community Member

                                Ok so Ive solved the continouos error when tabbing into next field ie radio button by placing validation in validation instead of calculate.

                                I think this is more user friendly now.

                                 

                                I think Im neally there with the submit, error message appears when 100 not exact in Total, yet it still allows form submission.

                                Im hoping that we can stop submission until 100 is in total field.

                                 

                                I didnt quite understand from your code // place here the submitForm call that you currently have in your file

                                 

                                Where do you think I'm going wrong?

                                 

                                New file (3rd time lucky)

                                 

                                https://acrobat.com/app.html#d=enKWzyjLq3EVNw*mYcL0ng