Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Calculating numbers from text values

Avatar

Level 2

Hello,

I am trying to get a form that will calculate the annual salary of an individual from a paycheck amount.  I have three fields: "paycheckAmount", "paymentFrequency", and "annualIncome".  The paycheckAmount field is set to be a number field.  The paymentFrequency field is a dropdown menu that give the options "Weekly", "Bi-Weekly", "Semi-Monthly", "Monthly", and "Annual".  Is there a way to have the form automatically calculate the annual income based on the input of the paymentFrequency and paycheckAmount fields?

Thank you in advance for help with this.

8 Replies

Avatar

Level 2

DropDownList2 is your income frequency, numericfield4 is your annualIncome and numericField3 is your checkAmount. Make sure that the following script is located on your annualIncome.

form1.#subform[0].NumericField4::calculate - (JavaScript, client)
switch(DropDownList2.rawValue){
  case "Weekly":
      this.rawValue=NumericField3.rawValue*52;
      break;
  case "Bi-weekly":
      this.rawValue=NumericField3.rawValue*26;
      break;
  case "Monthly":
      this.rawValue=NumericField3.rawValue*12;
      break;
  case "Annual":
      this.rawValue=NumericField3.rawValue;
      break;
  default:
      this.rawValue=0;
      }

MC

Avatar

Level 2

Thanks for your quick reply.  Below is the exact code that I have put in:

It is giving me an error on line 10, although I don't know what the problem is.  Also, it is not working when I test the form.

Avatar

Former Community Member

You are missing a colon after "Semi-Monthly" and "Monthly".

Steve

Avatar

Level 2

Thank you for catching that.  It still is not calculating the annual payment using the corrected code.


Avatar

Former Community Member

Using text fields for numbers makes me a bit squeamish so I tend to check for numbers (isNaN) and convert strings to integers (parseInt) or floats (parseFloat) depending upon the requirements. The following code produces an integer annual income.

// form1.page1.applicantAnnualIncome::calculate - (JavaScript, client)


if (!(form1.page1.ApplicantIncome.isNull)) {

  var incomeStr = ApplicantIncome.rawValue;

  if (isNaN(incomeStr)) {

    xfa.host.messageBox("Income is not a number.","Income Validation",1);

    this.rawValue = null;

  }

  else {

    var incomeInt = parseInt(incomeStr);

    switch (ApplicantFrequency.rawValue) {

      case "Weekly":

        this.rawValue = incomeInt * 52;

        break;

      case "Bi-Weekly":

        this.rawValue = incomeInt * 26;

        break;

      case "Semi-Monthly":

        this.rawValue = incomeInt * 24;

        break;

      case "Monthly":

        this.rawValue = incomeInt * 12;

        break;

      case "Annual":

        this.rawValue = incomeInt;

        break;

      default:

        break;

    }

  }

}

Steve

Avatar

Level 2

Thank you very much for your help.  I am still having issues getting that code to work.  I have downloaded the file you provided, and compared the files.  The only difference is that my data is held in a table.  Would that make a difference?

Avatar

Former Community Member

Yes. Please forward the form to stwalker.adobe@gmail.com and I can take a look.

Steve

Avatar

Level 2

The following code was used within the table:

if (!(xfa.resolveNode("form1.page1.income.table.row[0].applicantIncome").isNull)) {
    var income = xfa.resolveNode("form1.page1.income.table.row[0].applicantIncome").rawValue;
    if (isNaN(income)) {
        xfa.host.messageBox("The applicant income is not a number.","Income Validation",1);
        this.rawValue = null;
    }
    else {
        var freq = xfa.resolveNode("form1.page1.income.table.row[1].applicantIncomeFreq").rawValue;
        switch (freq) {
            case "Weekly":
                this.rawValue = income * 52;
                break;
            case "Bi-Weekly":
                this.rawValue = income * 26;
                break;
            case "Semi-Monthly":
                this.rawValue = income * 24;
                break;
            case "Monthly":
                this.rawValue = income * 12;
                break;
            case "Annual":
                this.rawValue = income;
                break;
            default:
                break;
        }
    }
}