7 Replies Latest reply on Feb 19, 2011 10:42 AM by gkaiseril

    Java Script

    burlingtonnow

      I would like a little help on my problem Please.

       

      STR is the field that has a decimal....say 1.99, now I know it will round up to a 2 but I want it to equal  1 in another field  called STR1.

       

      How do I calculate the field STR1.  I think the numbers should be parsed, or a parsed  string
      so I added a var fVal = parseFloat(STR); to my code.  Is this correct?

       

      var fVal = parseFloat(STR);

      if(STR < 1.99) {
                      value = 1;
      } else if(STR < 2.5) {
                      value = 2;
      } else if(STR < 3.5) {
                      value = 3;
      } else if(STR < 4.25) {
                      value = 4;
      } else {
                      value = 5;
      }

        • 1. Re: Java Script
          gkaiseril MVP & Adobe Community Professional

          Why parse to a floating number?

           

          You can parse to an integer or use Math.floor() method to truncate a floating point number to an integer.

           

          Run a JavaScript on the "Blur Action":

           

          // parse the integer from the floating point number STR

          this.getField("STR1").value = parseInt(event.value);

           

          or

           

          // turncate floating point number to integer

          this.getField("STR1").value = Matn.floor(event.value);

          • 2. Re: Java Script
            burlingtonnow Level 1

            Thank you for your help and advice, It was very uselpful .

             

            I have this problem now.....I have a field that calculates the number of days between two dates....That works fine. I have another field that adds the days in a column  called TotalDays, that also works fine. I also have another column that add up inputed number...called Open, that total in a field called OpeningDiv, which also works fine.  Now I have a AverageDay field that divided TotalDays/OpeningDiv......and it works. My problem is when I delete any numbers or dates from the columns..... I get this Warning Java Script Window....and it says

            THE VALUE ENTERED DOES NOT MATCH THE FORMAT OF THE FIELD [AverageDay]

             

            Any Ideas why I am getting the error message. Because when I want to input a date or any numbers or just start over I get this Warning Java Script window

            • 3. Re: Java Script
              gkaiseril MVP & Adobe Community Professional

              You are dividing by zero or a null string.

               

              What is the result of any division by zero?

               

              Change the format of the average field to "None" and observe the answer. It should be one of three possible text strings.

               

              Thanks for recognizing another answer.

              • 4. Re: Java Script
                maxwyss Level 4

                That format mismatch is a typical consequence of a division by zero, and formatting the result field as number.

                 

                If you take out the formatting for the result field, you will see either NaN or Infinity. NaN stands for "Not a Number", and this is, obviously not a number.

                 

                The workaround for this is to delay the calculation until you no longer have a division by zero. Note that a division by zero will in no case give you a valid result anyway, so the whole calculation is actually invalid. You would evaluate the value of the OpeningDiv field, and only do the calculation if it is != 0.

                 

                If you have subsequent calculations, you should delay them as well. It might be a good idea to consolidate all the calculations into one single script, where you have full control over the calculation, and you can make sure that the results will be valid (and this will also make your form faster).

                 

                HTH.

                 

                Max Wyss.

                • 5. Re: Java Script
                  heyviz Level 1

                  Again Thank you. You are making me look good. More over I am learning and that is what I appreciate most. Again Thank you.

                  • 6. Re: Java Script
                    burlingtonnow Level 1

                    Hi I have been spending alot of me time looking for delay javascript without any luck.

                    I need field TotalDays to calculate seperate and OpeningDiv to calculate. And I am looking for a script that will delay the division of TotalDays/OpeningDiv, in my AverageDiv field

                     

                     

                    I do understand that my form is dividing by o and it is giving me that Warning JavaScript....I just can't get me head around how to make it stop and do my division.

                     

                    Please advise

                    • 7. Re: Java Script
                      gkaiseril MVP & Adobe Community Professional

                      You use an 'if' statement to test if the value being used for the divisor is zero or a null string. And then if it is not either of those 2 values, perform the division, else clear the field. Be careful not to confuse the letter oh/Oh with the number 0.

                       

                      Thank you for marking the previous answers answered.

                       

                       

                      // define some field name values

                      var sDivisor =  "Text2"; // name for divisor field

                      var sDividend = "Text1"; // name for dividend field

                       

                      // do not change any code below this line

                      // clear the result field

                      event.value = ""; // suppress zero

                      // get the value of the divisor field

                      var nDivisor = this.getField(sDivisor).value;

                      // test for a non-zero and non-null divisor value

                      if(nDivisor != 0 & nDivisor.toString() != "") {

                        // then get dividend value

                        var nDividend = this.getField(sDividend).value;

                        // compute the result

                        event.value = nDividend / nDivisor

                        } // end of calculation