Skip to main content
New Participant
January 15, 2018
Answered

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

  • January 15, 2018
  • 2 replies
  • 2691 views

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!

This topic has been closed for replies.
Correct answer gkaiseril

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;

}

2 replies

gkaiserilCorrect answer
Inspiring
January 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;

}

PLMUAuthor
New Participant
January 16, 2018

Amazing!

Thank you both for the help!

try67gkaiseril

try67
Community Expert
January 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.