help with form calculation

New Here ,
May 13, 2022 May 13, 2022

Copy link to clipboard

Copied

I have tried a number of approaches to a calculation but keep getting NaN.

I have fields A that is a value in inches,B is a value in mm, C is a value in cm.

The user of the form only needs to fill out one of the fields and the other 2 will populate based on a calculation.

So, for example, if they fill in A, it will keep that value in A, but B and C will auto populate.

If they change any one of the values, the other fields will update.

Below is the most recent code which I added as a calculation for field A. It returned NaN.

Thanks for any help!

var A = this.getField("A").valueAsString; 
var B = this.getField("B").valueAsString; 
var C = this.getField("C").valueAsString;
if (A=="" && B=="") 
{
event.value = C.valueAsString / 2.54; 
} 
else if (A=="" && C=="") 
{
event.value = B.valueAsString / 25.4; 
} 
else
{
event.value = event.target.value;
}

It seems like it would make more sense to check B for a value and calculate if it has one, check c for a value and calculate if it has one, or do nothing (display what is entered). But I could not figure that out either.

 

Thank you for any help! 

TOPICS
JavaScript , PDF forms

Views

426

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
Adobe Community Professional ,
May 13, 2022 May 13, 2022

Copy link to clipboard

Copied

You can't divide string with a number, use like this:

var A = this.getField("A").valueAsString;
var B = this.getField("B").valueAsString;
var C = this.getField("C").valueAsString;
if (A=="" && B=="")
event.value = Number(C) / 2.54;
//...etc

 

What you trying to do here:

event.value = event.target.value;

if you want set value to be blank use like this: event.value = "";

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 ,
May 14, 2022 May 14, 2022

Copy link to clipboard

Copied

Thank you I'll try that. What I was trying to say at the end was, if someone enters a value into A, that should be the value. Not sure it is necessary though.

 

Is there a way to write, if a field is not blank, do this? It would be easier if I could say, "if A has any value, multiply it by 2.." etc. But I can't find a way to say that. 

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
Adobe Community Professional ,
May 14, 2022 May 14, 2022

Copy link to clipboard

Copied

Try like this:

if(A) event.value = Number(A)*2;

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
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

LATEST

If you do do it like that you should also add an else-clause, in case "A" is blank. In that case you probably want the field to be empty, too. Add the following after the line Nesa posted:

 

else event.value = "";

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