6 Replies Latest reply: Jul 10, 2014 10:53 AM by lmphil RSS

    JavaScript in a Text Field to Show/Hide a Calculated Field

    lmphil Community Member

      Thanks for your help. Here's the situation. I created a form with text fields, number (currency) fields, and calculated fields. The calculated fields display the currency symbol (even when there is nothing to calculate) and this causes a problem for our users who just want to print out the form and fill it out by hand (because the currency symbols get in the way.). So, I wanted to write a script the would make the calculated fields hidden unless there was actually something entered in the first text field (EmpID). This is what I came up with (TotalCompensationRow1 is a calculated field.):

       

      var TotalCompensationRow1Fld = this.getField("TotalCompensationRow1")

       

      if (event.value)  {
           TotalCompensationRow1Fld. display = display.hidden
      }

       

      else   {
           TotalCompensationRow1Fld. display = display.visible
      }

       

      Somehow, I think I'm making this much more difficult than it needs to be. Thanks again for your help.

        • 1. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
          George_Johnson CommunityMVP

          Assuming that's a validate script, it should work if you remove the spaces that are after the "." and before the word "display".

          • 2. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
            lmphil Community Member

            Thanks for the correction! I did adjust the script and doublechecked my field names, but the calculated field remains hidden after I enter text in the text box. Thanks again for your insight.

            • 3. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
              George_Johnson CommunityMVP

              Note that the validate script doesn't get triggered until the field value actually changes, to try that to see if it starts working.

              • 4. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
                GKaiseril CommunityMVP

                Another approach would be to modify the formatting of displayed field. When using a Percentage or Currency Symbol one cannot suppress the display of the "0.00%" or "$0.00". But if one use the Number format with no currency symbol one can suppress the "0.00" display.

                 

                Validation for Currency format:

                var nDec = 2; // set the number of decimal places; // number & percent;

                var sepStyle = 0; // thousand separator style; // number & percent;

                var negStyle = 0; // negative style; // number only;

                var currStyle =""; // always null; // number only;

                var strCurrency = ""; // currency symbol; // number only;

                var strCurrencyCurrency = "$"; // currency symbol; // number with currency symbol;;

                var bCurrencyPrepend = false; // currency prepend; // number only;

                var bPercentPrepend = false; // percentage prepend; // percent only;

                console.println(event.value);

                if(event.value == 0 || event.value == "") {

                event.value = "";

                AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend);

                } else {

                bCurrencyPrepend = true;

                AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrencyCurrency, bCurrencyPrepend);

                }

                 

                Validation for the Percentage format:

                 

                var nDec = 1; // set the number of decimal places; // number & percent;
                var sepStyle = 0; // thousand separator style; // number & percent;
                var negStyle = 0; // negative style; // number only;
                var currStyle =""; // always null; // number only;
                var strCurrency = ""; // currency symbol; // number only;

                var strCurrencyCurrency ="$"; // currency symbol // number with currency symbol;
                var bCurrencyPrepend = false; // currency prepend; // number only;
                var bPercentPrepend = false; // percentage prepend; // percent only;
                if(event.value == 0 || event.value == "") {
                event.value = "";
                AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend);
                } else {
                AFPercent_Format(nDec, sepStyle, bPercentPrepend);
                }

                • 5. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
                  lmphil Community Member

                  Thanks for your help! Below is a snapshot of the form code and another of the form filled in. The code you suggested worked beautifully except for the last field (GrandTotal)

                  Field Names (Edit View).jpg

                  Filled In Fields.jpg

                  The Grand Total (bottom right corner only displays 2 digits. All of the calculated fields are formatted to none with the validation code. Each of the Total fields are calculated fields. Any idea why the Grand Total is displaying differently?

                   

                  Thanks for all of your help.

                  • 6. Re: JavaScript in a Text Field to Show/Hide a Calculated Field
                    lmphil Community Member

                    Thanks for all of your help! After studying the fields, I realized that the issue appeared to be calculating two calculated fields. I made the Grand Total a calculation of all of the rows/columns (minus the Totals). I works just fine! Thanks again.