2 Replies Latest reply on Jun 14, 2013 9:58 AM by ghenry20

    Clearing NaN error from form fields.

    ghenry20

      Hey all,

       

      Working with this script I have and I thought my condition should clear the NaN erro but it does not.  Does anyone have a clear for the NaN error that might work better.  Basicully,  all the script does is the person using it puts military time into 2 different areas.  One "On" and one "Off".  The NaN error stays in the field until they person using the form puts in the 2 times.

       

      (function () {

       

          // Get the field values, as numbers

          var v1 = getField("HoursOFF1").value;

          var v2 = getField("HoursON1").value;

          var v3 = getField("HoursDif1").value;

                var time1 = v1.split(":");

                var time2 = v2.split(":");

                var min1 = time1[0]*60 + time1[1]*1 //

                var min2 = time2[0]*60 + time2[1]*1 ;

          if (v3 !== 0) {

             event.value = ((min1 - min2) / 60);

          } else {

              event.value = "";

          }

      })();

       

      If anyone can spot my error it would be much appreciated.  Thanks in advance!

        • 1. Re: Clearing NaN error from form fields.
          try67 MVP & Adobe Community Professional

          The split command returns strings, which you're then trying to use in a

          mathematical calculation, hence the NaN (Not a Number) errors.

          You need to explicitly convert those strings to numbers, like so:

           

          Number(time1[0])

          • 2. Re: Clearing NaN error from form fields.
            ghenry20 Level 1

            Thanks for the help!  I still had to twik it just a little bit more.  Final code follows:

             

            (function () {

             

                // Get the field values, as numbers

                var v1 = getField("HoursOFF1").value;

                var v2 = getField("HoursON1").value;

                      var time1 = v1.split(":");

                      var time2 = v2.split(":");

                      var min1 = Number(time1[0])*60 + Number(time1[1])*1 //

                      var min2 = Number(time2[0])*60 + Number(time1[1])*1 ;

                if (v1 != 0) {

                   event.value = ((min1 - min2) / 60);

                } else {

                    event.value = "";

                }

            })();