6 Replies Latest reply on May 30, 2016 12:02 PM by gkaiseril

    Calculation with a field and a dropdown

    antoinetchoung

      Hi,

      I want to create a Field that give me the amount of product needed according to the length of the site and of the box (selectable in a list)

       

      I've try that :

       

       

      var a = Number(this.getField("SiteSize").value);

      var b = Number(this.getField("DropdownBoxSize").value);

      if (b==0) event.value = "";

      else event.value = Math.floor(a/b);

       

       

      but it's not working.


      Any idea ?

        • 1. Re: Calculation with a field and a dropdown
          try67 MVP & Adobe Community Professional

          In what way is it not working? Where did you place the code? Are there any error messages in the JS console?

          • 2. Re: Calculation with a field and a dropdown
            antoinetchoung Level 1

            Nothing special, only a huge "NaN" message and nothing's changing when I change the Dropdown value

            • 3. Re: Calculation with a field and a dropdown
              try67 MVP & Adobe Community Professional

              You didn't answer my second question...

               

              This message means that the value of at least one of those fields can't be converted to a number. You need to go back and make sure that you only use numbers in your calculation.

              • 4. Re: Calculation with a field and a dropdown
                antoinetchoung Level 1

                The "nothing special" was for the JS station


                I've change the "numbers separator" (from 1.234,567 to 1234.567) and in fact now it's working (I did not understant why but it doesn't matter)


                Thanks for your time !

                • 5. Re: Calculation with a field and a dropdown
                  try67 MVP & Adobe Community Professional

                  If you're using the built-in Number format option that should not cause a problem.

                  But if you're just entering the number like that then it won't work if you don't use the correct notation.

                  • 6. Re: Calculation with a field and a dropdown
                    gkaiseril MVP & Adobe Community Professional

                    You have to do your homework and learn some basic information about data types.

                     

                    The two basic types of data. The first is which consist of alphabetical character, numbers, control characters, numbers. The other type is number which consist of the digits 0-9 the leading sign of "+" o r"-" and one decimal point. A null character, "", is treated like zero.. String character in general cannot be used in numeric calculation but to have some actions like concatenation, and sub setting (substring). Number values have the common 4 arithmetic operations, numeric comparisons, and many math functions as documented in the MDN JavaScript Reference.

                     

                    If you are going to perform a calculation you need know the data type for the various values. A space , " ", is not the same as a zero or a null string so one should be aware that the values are numbers. One can test for values that are Not a Number (NaN) by is the isNaN() constrictor if one is allowing non-numeric values as a possible input to their calculation.

                     

                    Using a text field with any of the format options will always only allow a numeric value to be entered. Using a drop down allows any string value to be entered, so it is up to the programmer to make sure the value provided by the drop down selection is a number. This is where the optional value for the drop down element comes into play. If one enters blank or space for the non-selected one could use a 0 for the value. If this is not done then your script must test for a non-numeric value and control the blocks of code that is executed.

                     

                    Using a non-numeric value in an arithmetic calculation will result in a NaN, Not a Number result.

                     

                    One could use a script like:

                     

                    var a = Number(this.getField("SiteSize").value);

                    var b = Number(this.getField("DropdownBoxSize").value);

                    // clear field value;

                    event.value = "";

                    // test b for NaN value;

                    if(isNaN(b) != true)

                    {

                    if(b != 0) {

                    event.value = Math.floor(a / b);

                    }