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

How to differentiate between a zero and a blank field in JavaScript

Explorer ,
Jun 21, 2023 Jun 21, 2023

Copy link to clipboard

Copied

I have a running total (TC) that I do not want to calculate (remain blank) until both parts of the calculation have been completed. To do this I have this custom calculation:

var a = Number(this.getField("TC.1").valueAsString);
var b = Number(this.getField("Ch.2").valueAsString);
if (b!="") {
event.value = a + b;
} else event.value = "";

 

This worked perfectly, until I have a zero value for var b. The zero is treated as a blank field ("") and the total does not calculate.

Note: CHG column = Ch. fields and TCHG=TC. fields in the tables below

slaron_0-1687375124102.pngexpand image

To try and rectify this, I changed the script to:

var a = Number(this.getField("TC.1").valueAsString);
var b = Number(this.getField("Ch.2").valueAsString);
if (b!="" || b==0) {
event.value = a + b;
} else event.value = "";

 

The above solved the calculation problem, but still equates a zero with a blank, so that the TC fields are calculated before var b has a value.

slaron_1-1687376005285.pngexpand image

Is there any way to rewrite the script so that a zero is considered a number to be calculated AND a blank fe field is not? Any help would be appreciated!

TOPICS
How to , JavaScript , PDF forms

Views

479
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
1 ACCEPTED SOLUTION
Community Expert ,
Jun 21, 2023 Jun 21, 2023

Copy link to clipboard

Copied

Use this:

 

var a = Number(this.getField("TC.1").valueAsString);
var b = this.getField("Ch.2").value;
if (typeof b !== "string") {
event.value = a + Number(b);
} else event.value = "";

 

View solution in original post

Votes

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
Community Expert ,
Jun 21, 2023 Jun 21, 2023

Copy link to clipboard

Copied

Use this:

 

var a = Number(this.getField("TC.1").valueAsString);
var b = this.getField("Ch.2").value;
if (typeof b !== "string") {
event.value = a + Number(b);
} else event.value = "";

 

Votes

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
Explorer ,
Jun 21, 2023 Jun 21, 2023

Copy link to clipboard

Copied

LATEST

Worked perfectly! Thank you so much Nesa--you're the best!

Votes

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