Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

if a field is blank, allow user input in another field?

Engaged ,
Mar 10, 2018 Mar 10, 2018

I have three fields, "OvertimeHrs", "OvertimeAmount" and "TotalPay".
OvertimeHrs is for user input and not calculated
OvertimeAmount is calculated by multiplying OvertimeHrs times a specific number, and is working:


var v1 = this.getField("OvertimeHrs").value;

event.value = 30 * v1;

TotalPay is calculated by adding OvertimeAmount to a specific number, and is also working:

var v2 = this.getField("OvertimeAmount").value;

event.value = 15 + v2;

Now I would like to edit the code, but I don't know the syntax. If the OvertimeHrs field is blank, I would like to have the other two fields to be empty and allow regular user input.  So in OvertimeAmount, it would something like:

if OvertimeHrs is not blank, do the calculation, else do nothing (leave box empty, no zero or anything)

And in TotalPay, if OvertimeHrs is not blank, do the calculation, else do not calculate and allow user input.

Is this possible, and if so could anyone tell me what the syntax would be for that? Thank you for your help!

TOPICS
PDF forms
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 12, 2018 Mar 12, 2018

Allowing manual entry is a different kind of issue. When a field is calculated the code is forcing a value. So any entry is fighting the built-in behavior. The solution is to add code that blocks the calculation from having an effect.

Here's how it works:

var v1 = this.getField("OvertimeAmount").value;

event.value = 15 + v2;

event.rc = (v1 != "");  // this blocks the calc

event.target.readonly = event.rc // This blocks or enables user data entry

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2018 Mar 10, 2018

Yes, it is possible.

All you need to do is to test the value of the input field to the calculation for an empty value.

For example:

var v1 = this.getField("OvertimeHrs").value;

if(v1 == "")

   event.value = "";

else

    event.value = 30 * v1;

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 11, 2018 Mar 11, 2018

Thank you, Thom!  This works perfectly for the OvertimeAmount field, but for the TotalPay field, Is there a way to tell it to completely ignore any code is the variable tests empty?  Right now it's forcing a blank in TotalPay when there's no value in the variable, but I would like it to allow user input in TotalPay when there's nothing in OvertimeHrs. Thank you for your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 12, 2018 Mar 12, 2018

Allowing manual entry is a different kind of issue. When a field is calculated the code is forcing a value. So any entry is fighting the built-in behavior. The solution is to add code that blocks the calculation from having an effect.

Here's how it works:

var v1 = this.getField("OvertimeAmount").value;

event.value = 15 + v2;

event.rc = (v1 != "");  // this blocks the calc

event.target.readonly = event.rc // This blocks or enables user data entry

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 12, 2018 Mar 12, 2018
LATEST

Thom, thank you so much!  That did the trick!   I appreciate your help!!!
ps- Both of your answers are correct, but I can only pick one... wish I could pick both!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines