need script to return blank field if input is zero

New Here ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

I'm using the following simplified notation to calculate a field: 

100-[(Text3/Text1)*100]

If the input for Text3 and Text1 = 0, the result = NaN

I need a script that will return a blank field if the input is zero instead of NaN. 

Thanks in advance for your help.

TOPICS
PDF forms

Views

44

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

Use this:

 

var v1 = Number(this.getField("Text3").valueAsString);
var v2 = Number(this.getField("Text1").valueAsString);
if (v2==0) event.value = "";
else event.value = 100-((v1/v2)*100);

Likes

Translate

Translate

Report

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
New Here ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

Thank you that worked. Can you help with another question? I am calcuating the total number of filled in fields but do not want to include blank fields in the total. I am calculating the total using the following script:

 

var total = "";
for (var i=1; i<=7; i++) {
if (this.getField("Total"+i).value != "") total++; }
event.value = total;

 

How can I keep the blank fields from being included in the total? Thanks again for your help.

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

This code should do that, although you could change value to valueAsString in the third line.

Also, change the initial value of total to 0, instead of ""...

Likes

Translate

Translate

Report

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
New Here ,
Mar 05, 2021 Mar 05, 2021

Copy link to clipboard

Copied

LATEST

Perfect! Thank you.

Likes

Translate

Translate

Report

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