3 Replies Latest reply on Oct 30, 2013 7:25 AM by gkaiseril

    Null vs. zero vs. ""

    IDOTRhonda Level 1

      I have a form with calculated fields that I am struggling with because sometimes the user will enter a zero (and they should).

       

      I have little experience with javascript so please excuse my ignorance.  I have been creating adobe forms with Acrobat Pro which originally resided in Microsoft Excel.  These are forms that are filled out by our employees and turned in with project files.  Many forms have multiple rows, a few simple calcuations and a running total.

       

      In many cases I have used something like this:

       

      // Get field value

      var v1 = getField("LengthRisersToday.1").value;

      var v2 =
      getField("LengthRisersToDate.0").value;

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

      else event.value = (v1 + v2);

       

      because I don't want the answer to be repeated on every single row when an entry has only been made on the first row (or the first few rows). 

       

      This time my solution has turned into a problem...Length of Risers Today may sometimes be zero and my "if" won't work if someone enters a zero.  It also resets the running total.

       

      This is what I want:

       

      Today To Date
      44
      04
      04
      37
      07

       

      This is what I get:

       

      TodayTo Date
      44
      0
      0
      33
      0

       

      I have tried adjusting this to

       

      if (v1=="") event.value = ""; 

      else event.value = (v1 + v2);

       

      if (v1==null) event.value = ""; 

      else event.value = (v1 + v2);

       

      I also tried a custom validation script:

       

      // Get field value

      var v1 = getField("LengthRisersToday.1").value;

      if (v1=="") event.value = "";

       

      and I tried

       

      if(event.value == 0) {

      event.value = "";

      } else {

      event.value = event.value

      }

       

      Nothing is working for me and I feel I am completely off track.

       

      Thanks for any advise!

        • 1. Re: Null vs. zero vs. ""
          gkaiseril MVP & Adobe Community Professional

          You have to understand about the different types of values. 

           

          You can have a value that has the type of "string", for example "one".

           

          You can have a value that has the type of "number", for example 1.

           

          You can have a value that has the type of string but looks like a number and under the right circumstance acts like a number, for example "1".

           

          Some sample code:

           

          console.println("A string: " + typeof "one");
          console.println ("A number: " + typeof 1);
          console.println("A number as a string: " + typeof String(1));
          console.println ("A number as a string: " + typeof "1");
          console.println ("A string as a number: " + typeof Number("1"));

           

           

          The value of a given field and the type of data is automatically determined by JavaScript unless one enforces or limits it to a given type.

           

          If you want to test for a null string value or null value for a numeric field you need to the "valueAsString" property. If you just use the "value" property for the field and compare to 0, then null values  or null strings will be automatically be converted to zero for the comparison.

           

          One also needs to be careful when adding a group of variables the consist of numbers and nulls since the "+" operator is both the additive operator and the string concatenation operator.

           

           

           

           

          • 2. Re: Null vs. zero vs. ""
            IDOTRhonda Level 1

            I have a basic understanding of the different types of values and I should not have written zero in my original post as it would be entered 0.

             

            I do not understand your sample code and googled console.printin and came up with an interesting article: http://acrobatusers.com/tutorials/javascript_console but it didn’t really help me to understand the sample code or how to use it. 

             

            I have read and re-read your answer and I appreciate what you are trying to explain to me but I don’t think I am being clear enough in what I am trying to accomplish.

             

            I don’t think I need to use the valueAsString property because the user would always enter the value as a number – these forms have been used for many years in Excel and they know what to enter.  I am not sure if this was unclear.

             

            All of my examples were my feeble attempt to get this calculation to work without listing the answer in every row on the form.

             

            To be more specific about the form, listed below are the fields  in the form.  Where is says number, it is entered by the user.  Where is says calculation, I have created a calculation.  The problem I am having is with the Length of Risers Added – each time they check the elevation they do not always add risers.  So there will be several entries with a 0 entered and that makes my calculation fail (if i use the "if statement"). 

             

            Elevation of top of plate as initially installed (number) (entered once)

            The following fields are headings and there are 18 rows on the form. 

            Date Read

            Fill Elevation at riser (number)

            Fill Height (calculation)

            Length of Risers Added

                            Today (number)

                            To Date (calculation)

            Elevation at top of riser (number)

            Today’s plate elevation (calculation)

            Current Settlement (calculation)

            Total Settlement (calculation)

             

            If I remove the part: 

            if(v1==0) event.value=””;

            Else

             

            The formula will work but the problem is the answer will be repeated on every row of the form from that point to the bottom.

             

            I'm sorry if I am not "getting" what you are trying to tell me, I really do want to learn to do this correctly.  I do not think I will ever do much more than calculations though and that might be part of the learning problem?

             

            • 3. Re: Null vs. zero vs. ""
              gkaiseril MVP & Adobe Community Professional

              If you are trying to see if a numeric field has not entry, that is not even 0, you need to use the "valueAsString" property since JavaScript will try to convert a value to a number for the equality operator or one of the operations performed by the "+" operator.

               

              If you want to check any field for a null string that might be formatted as number you should use the "valueAsString" property and compare to the "" value.

               

              Also you should be aware to be able to show leading zeros for the SSN or Zip Code one also needs to use the "valueAsStirng" property or the leading zeros will be lost.