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

Using .value vs .valueAsString

Participant ,
Dec 14, 2019 Dec 14, 2019

Please help me understand when to use .valueAsString instead of just .value.  Below is the calculation I used (in black), with the portions in red added when I asked for help from this group in making it work.  Since this is a form to be completed either manually or as a fill-in, I also used a validation script on A and B fields: if(event.value == 0) event.value = "";.  The second part of my question, does it hurt to use vAS even when you don't need it?

 

I read the JS Reference and I think I understand "It differs from value, which attempts to convert the contents of a field contents to an accepted format. For example, for a field with a value of “020”, value returns the integer 20, while valueAsString returns the string “020”. 

 

Here's my thought - showing fields A & B as .valueAsString changed it to a undetermined format, which then had to be manually shown to be a number by the addition of A=Number(A).  But why did it need to be done in the first place?  Was it something to do with the "" blanks or validation, or maybe number & text in same statement?  Understanding why will help my in future queries.

 

If you can "dumb it down" for me into a beginner's explanation, I would greatly appreciate it -LOL!

TIA ~ Michelle

 

var A = this.getField("CD#Expect").valueAsString; // Expected Activity for period reviewed;

var B = this.getField("CD#MoTotal").valueAsString; // Actual Activity for period reviewed;

 

if (A=="" && B=="") event.value = "";

else {

        A = Number(A);

        B = Number(B);

        if (B>A) event.value = "Exceeds";

        else if (B<A) event.value = "Under";

        else if (B==A) event.value = "Meets";

}

 

TOPICS
Acrobat SDK and JavaScript
4.6K
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 ,
Dec 15, 2019 Dec 15, 2019

Hi,

 

In edit mode, right click on the Field that you have the custom calculation script and go to the format tab.

 

See if you have selected to have a zero to the left of the decimal.

 

You may  also choose "None" from the pulldown menu To assign no numerical format to this field and use something like Math.round or Math.ceil in your equation instead to handle the rounding fractional portion of your totals.

 

Also, I think that the validation line of code you can move it together with the custom calculation script (dont fully quote me on this one, I may be totally wrong. I am also learning).

 

But to answer your main question, if you have calculations that reference to a null value you cannot use valueAsString. You need to use value instead.

 

See better explanation here about converting  toString method: 

https://stackoverflow.com/questions/3945202/whats-the-difference-between-stringvalue-vs-value-tostri... 

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 ,
Dec 15, 2019 Dec 15, 2019
LATEST

The main difference is this: With valueAsString you know what you're getting back, ie a string. With value you don't know. It can be a string, or a number or even a boolean or an array. JavaScript is very flexible and does automatic type casting for you, but it's better to know what you're working with.

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