Copy link to clipboard
Copied
In short, I want to assign a value of zero to a dropdown field if the user does not choose an item from the list and enters custom text.
I have a form with multiple fields. The form calculates the Wheel Tax based on input from three fields. "Wheel Tax Cities" and "Wheel Tax Counties" are both dropdowns that have items with Export Values and allow user to enter custom text. A third field is called "License Plate Fee Dropdown." One of the fields called "Wheel Tax" I have a custom calculation script that sort of works but only calculated sporadically:
var t1 = getField("Wheel Tax Cities").value;
var t2 = getField("Wheel Tax Counties").value;
var PlateTransfer = getField("License Plate Fee Dropdown").value;
if (PlateTransfer < 1) event.value = 0;
else if (isNaN(t1)) t1 = 0;
else if (isNaN(t2)) t2 = 0;
else event.value = t1+t2;
Wheel Tax = Wheel Tax Cities + Wheel Tax Counties but is zero when there is a PlateTransfer.
Thanks for your time, and one last thing, is there a good reference book to buy to help me learn Acrobat Javascript? Thanks!!
2 Correct answers
Try this:
var t1 = Number(this.getField("Wheel Tax Cities").valueAsString);
var t2 = Number(this.getField("Wheel Tax Counties").valueAsString);
var PlateTransfer = Number(this.getField("License Plate Fee Dropdown").valueAsString);
if (PlateTransfer < 1 || isNaN(t1) || isNaN(t2)) event.value = 0;
else event.value = t1+t2;
You didn't cover the scenario when both values are not numbers.
You can do that by replacing these lines:
else if (isNaN(t1)) event.value = t2;
else if (isNaN(t2)) event.value = t1;
With this:
else if (isNaN(t1) && isNaN(t2)) event.value = 0;
else if (isNaN(t1) && !isNaN(t2)) event.value = t2;
else if (!isNaN(t1) && isNaN(t2)) event.value = t1;
Edit: fixed wrong spelling of isNaN in some instances...
Copy link to clipboard
Copied
Do you want it to show zero if any of the fields have a custom value? Because that's not what you're doing in the code above.
Copy link to clipboard
Copied
Only Wheel Tax Cites and Wheel Tax Counties allow for custom value. If either has a custom value then that one should read as zero.
When I select a city from the selection list in Wheel Tax Cities dropdown or select a city from the selection list in Wheel Tax Counties I need Wheel Tax to sum the two.
When I manually enter a city (custom value) in Wheel Tax Cities dropdown I need it to read as zero. Same for Counties.
If there is a plate transfer then there is no Wheel Tax and that field should then disregard any fees from either Cities or Counties and the calculation should be zero.
My License Plate Fee Dropdown looks like this:
My Wheel Tax Cities dropdown looks like this:
Wheel Tax looks like this:
Copy link to clipboard
Copied
Try this:
var t1 = Number(this.getField("Wheel Tax Cities").valueAsString);
var t2 = Number(this.getField("Wheel Tax Counties").valueAsString);
var PlateTransfer = Number(this.getField("License Plate Fee Dropdown").valueAsString);
if (PlateTransfer < 1 || isNaN(t1) || isNaN(t2)) event.value = 0;
else event.value = t1+t2;
Copy link to clipboard
Copied
Wow, I'm getting closer. I wasn't being clear with my precise need, sorry. If there is a plate transfer then Wheel Tax =0. If not, then use the event value of Cities AND Counties. If there is a custom value entered for Cites then return 0 for Cities but still use the value from Counties. And visa versa. If both Cities AND Counties are a custom value then Wheel Tax=0.
He're is what almost worked, but when I enter a custom value for BOTH Cites AND Counties I get "NaN":
var t1 = Number(this.getField("Wheel Tax Cities").valueAsString);
var t2 = Number(this.getField("Wheel Tax Counties").valueAsString);
var PlateTransfer = Number(this.getField("License Plate Fee Dropdown").valueAsString);
if (PlateTransfer < 1) event.value = 0;
else if (isNaN(t1)) event.value = t2;
else if (isNaN(t2)) event.value = t1;
else event.value = t1+t2;
I've never used the
Number(xxxxxxx).valueAsString)
I now see how to use it. "Number(value)" constrictor will (i think) convert a number character string to a floating point number and force JavaScript to perform addition and not concatenation.
Thanks!!
Copy link to clipboard
Copied
Yes, that's correct. It will try to convert the value (whatever type it might be) to a Number. If it fails, the result will be NaN, which you can detect using the isNaN method.
Glad to hear you got it working!
Copy link to clipboard
Copied
Sorry I didn't think you would reply to quickly. My new script is off just a little and I need just a little more help please. See above. I just edited it after I tested my form.
Copy link to clipboard
Copied
You didn't cover the scenario when both values are not numbers.
You can do that by replacing these lines:
else if (isNaN(t1)) event.value = t2;
else if (isNaN(t2)) event.value = t1;
With this:
else if (isNaN(t1) && isNaN(t2)) event.value = 0;
else if (isNaN(t1) && !isNaN(t2)) event.value = t2;
else if (!isNaN(t1) && isNaN(t2)) event.value = t1;
Edit: fixed wrong spelling of isNaN in some instances...
Copy link to clipboard
Copied
I replaced script with:
var t1 = Number(this.getField("Wheel Tax Cities").valueAsString);
var t2 = Number(this.getField("Wheel Tax Counties").valueAsString);
var PlateTransfer = Number(this.getField("License Plate Fee Dropdown").valueAsString);
if (PlateTransfer < 1) event.value = 0;
else if (isNan(t1) && isNaN(t2)) event.value = 0;
else if (isNan(t1) && !isNaN(t2)) event.value = t2;
else if (!isNan(t1) && isNaN(t2)) event.value = t1;
else event.value = t1+t2;
and now my javascript debugger is yelling at me. It says:
undefined
TypeError: this.getField(...) is null
4:Field:Calculate
TypeError: this.getField(...) is null
4:Field:Calculate
TypeError: this.getField(...) is null
4:Field:Calculate
TypeError: this.getField(...) is null
4:Field:Calculate
TypeError: this.getField(...) is null
4:Field:Calculate
SyntaxError: syntax error
5:
SyntaxError: syntax error
5:
SyntaxError: syntax error
5:
TypeError: t1.map is not a function
4:Field:Calculate
TypeError: t1.map is not a function
4:Field:Calculate
TypeError: t1.map is not a function
4:Field:Calculate
TypeError: t1.map is not a function
4:Field:Calculate
undefined
SyntaxError: missing ( after for
4:
SyntaxError: missing ( after for
4:
ReferenceError: isNan is not defined
5:Field:Calculate
ReferenceError: isNan is not defined
5:Field:Calculate
ReferenceError: isNan is not defined
5:Field:Calculate
Copy link to clipboard
Copied
Disregard, I found it. My bad I should have double checked. Lower case N messed it up.Thanks again I can't tell you how much I appreciate the help and how much I've learned:
var t1 = Number(this.getField("Wheel Tax Cities").valueAsString);
var t2 = Number(this.getField("Wheel Tax Counties").valueAsString);
var PlateTransfer = Number(this.getField("License Plate Fee Dropdown").valueAsString);
if (PlateTransfer < 1) event.value = 0;
else if (isNaN(t1) && isNaN(t2)) event.value = 0;
else if (isNaN(t1) && !isNaN(t2)) event.value = t2;
else if (!isNaN(t1) && isNaN(t2)) event.value = t1;
else event.value = t1+t2;

