• help with form calculation

# help with form calculation

New Here ,
May 13, 2022 May 13, 2022

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
4 Replies 4
May 13, 2022 May 13, 2022

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

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
May 14, 2022 May 14, 2022

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

Report

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

Copied

Try like this:

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

Likes

Report

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

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

Report

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