9 Replies Latest reply on Apr 3, 2014 3:35 PM by natjnelson

    Return Lowest Value of Two Fields

    natjnelson

      Hi,

       

      I have two fields that users manually enter into a form.  Value1 and Value2.

       

      Does anyone suggestions on the best way to use JavaScript to return the lowest value of the two fields?

       

      Thanks,

      Natalie

        • 1. Re: Return Lowest Value of Two Fields
          George_Johnson MVP & Adobe Community Professional

          Return the number to what and when? What should happen if either of the input fields is blank?

          • 2. Re: Return Lowest Value of Two Fields
            natjnelson Level 1

            There is another field in the form-LowestValue1.  In LowestValue1, I want to display the lowest value for fields Value1 and Value 2.

             

             

            Sorry for not providing this information earlier.

            • 3. Re: Return Lowest Value of Two Fields
              George_Johnson MVP & Adobe Community Professional

              OK, so you want LowestValue1 to be a calculated field. You didn't answer the second question, which is important.

              • 4. Re: Return Lowest Value of Two Fields
                gkaiseril MVP & Adobe Community Professional

                1. What do you want to happen if both fields are blank?

                 

                2. What do you want to happen if one field is blank?

                 

                3. If both fields are not blank - compute the minimum value.

                • 5. Re: Return Lowest Value of Two Fields
                  natjnelson Level 1

                  Yes.  I get why the second question is important. 

                   

                  I have instructions in the form telling the user to enter "None" if a value is not available for Value1 and Value2.  That brings up another question I guess.  Do Value1 and Value2 have to be formatted a Numbers?  Currently they are just text.

                   

                  I need the number in LowestValue1, not the "None."

                   

                  Again, if there is a better way to do this, I'm happy to learn and try!

                  • 6. Re: Return Lowest Value of Two Fields
                    George_Johnson MVP & Adobe Community Professional

                    They don't have to be formatted as numbers, the script can check if they are, and act accordingly. Something like:

                     

                    // Custom calculate script

                    (function () {

                     

                        // Get the field values, as strings

                        var s1 = getField("Value1").valueAsString;

                        var s2 = getField("Value2").valueAsString;

                       

                        // Attempt to convert to numbers

                        var v1 = +s1;

                        var v2 = +s2

                     

                        // Set to "None" if either field is blank or not a number

                        // Otherwise, set to the smaller number

                        if (!s1 || !s2 || isNaN(v1) || isNaN(v2)) {

                            event.value = "None";

                        } else {

                            event.value = Math.min(v1, v2);

                        }

                       

                    })();

                    • 7. Re: Return Lowest Value of Two Fields
                      natjnelson Level 1

                      Now that I am testing usability a little, I am wondering if I can save people a step and let them leave it blank (not make them enter None).  That would be more user friendly I think...

                       

                      Could the script be changed to return v1 if v2 is blank and v2 if v1 is blank?

                       

                      I tried this below, but I'm sure the set and otherwise is not quite right...

                       

                      // Custom calculate script

                      (function () {

                       

                          // Get the field values, as strings

                          var s1 = getField("Value1").valueAsString;

                          var s2 = getField("Value2").valueAsString;

                         

                          // Attempt to convert to numbers

                          var v1 = +s1;

                          var v2 = +s2

                       

                          // Set to "None" if either field is not a number

                          // Set to v1 if v2 is blank

                          // Set to v2 if v1 is blank

                          // Otherwise, set to the smaller number

                          if (!s1 || !s2 || isNaN(v1) || isNaN(v2)) {

                              event.value = "None";

                          } else {

                              event.value = Math.min(v1, v2);

                          }

                         

                      })();

                      • 8. Re: Return Lowest Value of Two Fields
                        George_Johnson MVP & Adobe Community Professional

                        I think I misunderstood what you meant by None, I thought you wanted the calculated value to be None. Set both of the input fields to number formats and change the script to:

                         

                        // Custom calculate script

                        (function () {

                         

                            // Initialize array

                            var aNums = [];

                         

                            // Get the field values, as strings

                            var s1 = getField("Value1").valueAsString;

                            var s2 = getField("Value2").valueAsString;

                         

                            // Convert values to numbers if not blank and add to array

                            if (s1) aNums.push(+s1);

                            if (s2) aNums.push(+s2);

                         

                            // Set this field's value to lesser of the inputs

                            // If both are blank, blank this field

                            event.value = aNums.length ? Math.min.apply(null, aNums) : "";

                         

                        })();

                        • 9. Re: Return Lowest Value of Two Fields
                          natjnelson Level 1

                          Wonderful!  Thank you for your assistance!