9 Replies Latest reply on Nov 29, 2011 12:23 PM by ioanah1503

    Trouble with java script

    ioanah1503

      Hello,

      I created a form that has a couple of simple calculations at the bottom. I have zero java script experience so any help is appreciated.

      The way the script is written right now gives me the error"The value entered does not match the format of the field [Total]"

      This is an out of pocket expense reimbursement form and i am trying to have it show the total that's due to the employee in the [Total] line. The form has to take whatever is entered in the "calculate.0; calculate.1...." and multiply that number by 0.55 which is the mileage reimbursement rate. In the "numbers" fields the user will be able to enter dollar amounts for other expenses such as tolls and parking.

      The [Total] line should take each of the "calculate" fields and multiply them by 0.55 and then add that total to the "numbers" values.

      PLEASE HELP!

      Thank you!!!!

       

      event.value= ( this.getField("calculate.0").value) * 0.55+

      ( this.getField("calculate.1").value) * 0.55+

      ( this.getField("calculate.2").value) * 0.55+

      ( this.getField("calculate.3").value) * 0.55+

      ( this.getField("calculate.4").value) * 0.55+

      ( this.getField("calculate.5").value) * 0.55+

      ( this.getField("calculate.6").value) * 0.55+

      ( this.getField("calculate.8").value) * 0.55+

      ( this.getField("calculate.9").value) * 0.55+

      ( this.getField("calculate.10").value) * 0.55+

      ( this.getField("calculate.11").value) * 0.55+

      ( this.getField("calculate.12").value) * 0.55+

      ( this.getField("calculate.13").value) * 0.55+

       

       

      parseInt(this.getField("numbers.0").value)+

      parseInt(this.getField("numbers.0.0").value)+

      parseInt(this.getField("numbers.0.1").value)+

      parseInt(this.getField("numbers.0.1.0").value)+

      parseInt(this.getField("numbers.0.1.0.0").value)+

      parseInt(this.getField("numbers.0.1.0.1").value)+

      parseInt(this.getField("numbers.0.1.0.1.2").value)+

      parseInt(this.getField("numbers.0.1.0.1.3").value)+

      parseInt(this.getField("numbers.0.1.0.1.4").value)+

      parseInt(this.getField("numbers.0.1.0.1.5").value)+

      parseInt(this.getField("numbers.0.1.0.1.6").value)+

      parseInt(this.getField("numbers.0.1.0.1.7").value)+

      parseInt(this.getField("numbers.0.1.0.1.8").value)+

      parseInt(this.getField("numbers.0.1.0.1.9").value)+

      parseInt(this.getField("numbers.0.1.0.1.10").value);

        • 1. Re: Trouble with java script
          ioanah1503 Level 1

          Or if i write it like this, it will accept the values in the field but it does not calculate the sum properly.

          <input class="required validate-number" id="Total" name="Total" />

          event.value= ( this.getField("calculate.0").value) * 0.55+

          ( this.getField("calculate.1").value) * 0.55+

          ( this.getField("calculate.2").value) * 0.55+

          ( this.getField("calculate.3").value) * 0.55+

          ( this.getField("calculate.4").value) * 0.55+

          ( this.getField("calculate.5").value) * 0.55+

          ( this.getField("calculate.6").value) * 0.55+

          ( this.getField("calculate.8").value) * 0.55+

          ( this.getField("calculate.9").value) * 0.55+

          ( this.getField("calculate.10").value) * 0.55+

          ( this.getField("calculate.11").value) * 0.55+

          ( this.getField("calculate.12").value) * 0.55+

          ( this.getField("calculate.13").value) * 0.55+

          (this.getField("numbers.0").value)+

          (this.getField("numbers.0.0").value)+

          (this.getField("numbers.0.1").value)+

          (this.getField("numbers.0.1.0").value)+

          (this.getField("numbers.0.1.0.0").value)+

          (this.getField("numbers.0.1.0.1").value)+

          (this.getField("numbers.0.1.0.1.2").value)+

          (this.getField("numbers.0.1.0.1.3").value)+

          (this.getField("numbers.0.1.0.1.4").value)+

          (this.getField("numbers.0.1.0.1.5").value)+

          (this.getField("numbers.0.1.0.1.6").value)+

          (this.getField("numbers.0.1.0.1.7").value)+

          (this.getField("numbers.0.1.0.1.8").value)+

          (this.getField("numbers.0.1.0.1.9").value)+

          (this.getField("numbers.0.1.0.1.10").value);

           

          Please help! I've been trying not to pull my hair out all day, but i'm just about to start.

          Thank you.

          • 2. Re: Trouble with java script
            George_Johnson MVP & Adobe Community Professional

            Set the format for the field to "None" to see what the result is. What format do you currently have it set to?

            • 3. Re: Trouble with java script
              George_Johnson MVP & Adobe Community Professional

              Which field is the "input field" that you mentioned?

               

              Are you doing this in Acrobat or LiveCycle Designer? In any case, what is this for: <input class="required validate-number" id="Total" name="Total" />

              • 4. Re: Trouble with java script
                ioanah1503 Level 1

                I had it set to number, changed it to none and it works file for the "calculate" fields but as soon as a i put something in the "number" field, it concatenates instead of adding the numbers.

                Thank you so much for your reply.

                • 5. Re: Trouble with java script
                  ioanah1503 Level 1

                  both calculate and number are fields that the user types stuff into. i' using acrobat pro.

                  • 6. Re: Trouble with java script
                    ioanah1503 Level 1

                    oops, that first line was an attempt to fix it, but clearly didnt work. i took it out.

                    • 7. Re: Trouble with java script
                      George_Johnson MVP & Adobe Community Professional

                      The problem is when you get the value of a blank field, it returns a string, which empty, but still a string. You can avoid the problem you're having by explicitly converting each value to a number, which you can do with the unary + operator. Also, parseInt will return the special value of NaN when the argument is a zero length string. So you could change the code to:

                       

                      event.value= ( +this.getField("calculate.0").value) * 0.55+

                      ( +this.getField("calculate.1").value) * 0.55+

                      ( +this.getField("calculate.2").value) * 0.55+

                      ( +this.getField("calculate.3").value) * 0.55+

                      ( +this.getField("calculate.4").value) * 0.55+

                      ( +this.getField("calculate.5").value) * 0.55+

                      ( +this.getField("calculate.6").value) * 0.55+

                      ( +this.getField("calculate.8").value) * 0.55+

                      ( +this.getField("calculate.9").value) * 0.55+

                      ( +this.getField("calculate.10").value) * 0.55+

                      ( +this.getField("calculate.11").value) * 0.55+

                      ( +this.getField("calculate.12").value) * 0.55+

                      ( +this.getField("calculate.13").value) * 0.55+

                       

                       

                      parseInt(+this.getField("numbers.0").value)+

                      parseInt(+this.getField("numbers.0.0").value)+

                      parseInt(+this.getField("numbers.0.1").value)+

                      parseInt(+this.getField("numbers.0.1.0").value)+

                      parseInt(+this.getField("numbers.0.1.0.0").value)+

                      parseInt(+this.getField("numbers.0.1.0.1").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.2").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.3").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.4").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.5").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.6").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.7").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.8").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.9").value)+

                      parseInt(+this.getField("numbers.0.1.0.1.10").value);

                      1 person found this helpful
                      • 8. Re: Trouble with java script
                        gkaiseril MVP & Adobe Community Professional

                        That is because there is difference between "Number as Text" and "Numbers". JavaScript uses the same operator, "+", for addition and concatenation. You need to make sure you are accessing a number. You and use the "Number" constrictor or use the multiplicative identity. JavaScript also tries to guess which action to use.

                         

                        event.value= ( this.getField("calculate.0").value * 0.55) +

                        ( this.getField("calculate.1").value * 0.55) +

                        ( this.getField("calculate.2").value * 0.55) +

                        ( this.getField("calculate.3").value * 0.55) +

                        ( this.getField("calculate.4").value * 0.55) +

                        ( this.getField("calculate.5").value * 0.55) +

                        ( this.getField("calculate.6").value) * 0.55 +

                        ( this.getField("calculate.8").value * 0.55) +

                        ( this.getField("calculate.9").value * 0.55) +

                        ( this.getField("calculate.10").value * 0.55) +

                        ( this.getField("calculate.11").value * 0.55) +

                        ( this.getField("calculate.12").value * 0.55) +

                        ( this.getField("calculate.13").value * 0.55) +

                        Number(this.getField("numbers.0").value) +

                        Number(this.getField("numbers.0.0").value) +

                        Number(his.getField("numbers.0.1").value) +

                        Number(this.getField("numbers.0.1.0").value) +

                        Number(this.getField("numbers.0.1.0.0").value) +

                        Number(this.getField("numbers.0.1.0.1").value) +

                        Number(this.getField("numbers.0.1.0.1.2").value) +

                        Number(this.getField("numbers.0.1.0.1.3").value) +

                        Number(this.getField("numbers.0.1.0.1.4").value) +

                        (1 * this.getField("numbers.0.1.0.1.5").value) +

                        (1 * this.getField("numbers.0.1.0.1.6").value) +

                        (1 * this.getField("numbers.0.1.0.1.7").value) +

                        (1 * this.getField("numbers.0.1.0.1.8").value) +

                        (1 * this.getField("numbers.0.1.0.1.9").value) +

                        (1 * this.getField("numbers.0.1.0.1.10").value);

                        • 9. Re: Trouble with java script
                          ioanah1503 Level 1

                          You are fantastic! Thank you sooooooooo much!

                          I'm going to spend the rest of the week watching lynda.com java script tutorials. it would help me so much to know how to do these things.

                           

                          It worked and i thank you a whole lot! You saved my computer from getting thrown through the window.

                          Thank you thank you thank you!