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

Returning different value if field is "0" to when it is empty

New Here ,
Jan 14, 2018 Jan 14, 2018

Hi all! 

I'm trying to create a script in Adobe Forms where variable in an adjacent field will return different values in the target field.  I would like the returning value to stay empty if the adjacent field is empty [if (v = empty) event.value = ""].  I've attempted to read through other answers, but haven't been successful in applying to this scenario. 

var v = Number(this.getField("ATPRow1").value);

if (v >= 0.01 && v <= 14.99) event.value = 2.75;

if (v  >= 15 && v <= 74.99) event.value = 3.85;

if (v >= 75) event.value = 5.06;

if (v == 0) event.value = 0.88;

Can anyone help?  Thanks in advance!

TOPICS
PDF forms
2.4K
Translate
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
LEGEND ,
Jan 15, 2018 Jan 15, 2018

A null string, "", under some situations is treated like zero in JavaScript. The only way to access the null value is to use the "valueAsString" property or the "String" constrictor.

var v = this.getField("ATPRow1").valueAsString;

if(v == "") event.value = "";

else {

     v = Number(v);

     if (v >= 0.01 && v <= 14.99) event.value = 2.75;

     if (v  >= 15 && v <= 74.99) event.value = 3.85;

     if (v >= 75) event.value = 5.06;

     if (v == 0) event.value = 0.88;

}

View solution in original post

Translate
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 ,
Jan 15, 2018 Jan 15, 2018

In the code above the variable "v" will never be an empty string because you're explicitly using the Number constructor, and Number("") will return zero. You need to first check if the value of the field (use valueAsString instead of value, by the way) is an empty string, and if it's not proceed to convert it to a number and perform the rest of your logic.

Translate
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
LEGEND ,
Jan 15, 2018 Jan 15, 2018

A null string, "", under some situations is treated like zero in JavaScript. The only way to access the null value is to use the "valueAsString" property or the "String" constrictor.

var v = this.getField("ATPRow1").valueAsString;

if(v == "") event.value = "";

else {

     v = Number(v);

     if (v >= 0.01 && v <= 14.99) event.value = 2.75;

     if (v  >= 15 && v <= 74.99) event.value = 3.85;

     if (v >= 75) event.value = 5.06;

     if (v == 0) event.value = 0.88;

}

Translate
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 ,
Jan 15, 2018 Jan 15, 2018
LATEST

Amazing!

Thank you both for the help!

try67​gkaiseril​

Translate
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