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

Validating Two Numbers with Javascript

New Here ,
Apr 17, 2019 Apr 17, 2019

Hello guys, I am really rookie with Javascript but I'd like to know what is wrong with this code?

I get alerted but not exactly on the numbers. I have two TOTAL values that I compare and I want to be alerted if they're the same or different.
Can anyone take a look and perfect this code, will be appreciated.

var V1=Number(this.getField("A").value);

var V2=Number(this.getField("B").value);

if(V1==V2)

{

app.alert("EQUAL");

}

else if (V1 >> V2){

app.alert("First value is bigger");

}

else {

app.alert("First value is smaller");

}

TOPICS
Acrobat SDK and JavaScript , Windows
792
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 ,
Apr 17, 2019 Apr 17, 2019

Replace ">>" 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
New Here ,
Apr 17, 2019 Apr 17, 2019

Nope, same outcome.

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 ,
Apr 17, 2019 Apr 17, 2019

What is the outcome, exactly, and where did you place the code?

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 ,
Apr 17, 2019 Apr 17, 2019

I placed the code exactly in the Validation menu of a text field where I put numbers.

So I have like 6 number fields that create a total of "A" and 6 different number fields that create a total of "B".

Then I put this code in both Validation menu columns of number field "A" and text field "B".

To check if my code is correct I change the 6 numbers so that "A" and "B" are equal, bigger or smalle than each other.

Does it make sense?

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 ,
Apr 17, 2019 Apr 17, 2019

Run it from the JS Console, instead.

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 ,
Apr 17, 2019 Apr 17, 2019

Here you can see what is happening. The two main values are different and Acrobat says they're equal... or when they're the same it says they "First value is bigger". Doesn't make sense to me.https://streamable.com/mvtl1

bandicam 2019-04-17 14-38-21-836 - Streamable

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 ,
Apr 17, 2019 Apr 17, 2019

You didn't apply any of my suggestions. You're still using an incorrect operator and are running the code from the validation event. There is a reason why I suggested you run it from the console window. If you don't want to listen that's fine, but then I can't help you further.

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 ,
Apr 17, 2019 Apr 17, 2019

Haha ye I just noticed as well.

EDIT: I removed the >


But look what it does. When the first total bigger with 1 digit it says it's still equal. Until it differs with 3 digits (numbers)


Check this video out: bandicam 2019-04-17 14-48-25-245 - Streamable

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 ,
Apr 17, 2019 Apr 17, 2019

Run the code from the JS Console and you'll see that it works.

The reason it's not working from the validation event is that when you access the value via getField("").value it returns the old value, as the new one hasn't been applied yet (you're still validating it at that moment). The new value can be accessed via event.value .

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 ,
Apr 17, 2019 Apr 17, 2019

Ohh so should it be like this?:

var V1=Number(event.value = "A");

var V2=Number(event.value = "B");

Nah that didn't work either. Then the value in the number field disappears. Could you help me out here?

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 ,
Apr 17, 2019 Apr 17, 2019
LATEST

You're again using the wrong operators... You really need to study the basic JS syntax.

It's "==", not "=".

And you only need to do that in the field where the code is located.
So for field A it will be:

var V1=Number(event.value);

var V2=Number(this.getField("B").value);

And for field B:

var V1=Number(this.getField("A").value);

var V2=Number(event.value);

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