Validating Two Numbers with Javascript

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Views

83

Likes

Translate

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

Validating Two Numbers with Javascript

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Views

84

Likes

Translate

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
Apr 17, 2019 0
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

Replace ">>" with ">".

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

Nope, same outcome.

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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?

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

Run it from the JS Console, instead.

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

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 .

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 2
New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

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?

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 0
try67 LATEST
Most Valuable Participant ,
Apr 17, 2019

Copy link to clipboard

Copied

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);

Likes

Translate

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
Reply
Loading...
Apr 17, 2019 2