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

Data in multiple fields conflicting each other in javascript

New Here ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

I have multiple fields to enter data. I am trying to VALIDATE that with results like ALERT and COLOR CHANGE in the same field where the data was entered. It is working fine up to certain range. But when the goes within the range of other field it is giving conflicting result.

Suppose I put the following in one field:

event.rc = true;
if (event.value >= "5" || event.value <= "10.0")
{
event.target.textColor = color.blue;
}
else
{
app.alert("The entered value is after lunch glucose and should be between '5.0' to '10.0'");
event.target.textColor = color.red;
}

 

 

And in another field I put:

event.rc = true;

if (event.value >= "90" || event.value <= "130.00" )

{
event.target.textColor = color.blue;
}
else
{
app.alert("The value is blood pressure SYS. If it goes higher than '130' you are in high blood pressure and if it goes lower than '90' you are in low blood pressure ");
event.target.textColor = color.red;
}

 

Thanks

TOPICS
Acrobat SDK and JavaScript

Views

712

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
community guidelines
Community Expert ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Why does you use || ?

Votes

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
community guidelines
New Here ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Thanks for your responding. I want both the conditions to be valid. I know
it should be &&, but if I put && it doesn't work at all. Then I put ||, in
this time it is working for some range in between but conflicting with
other data field.
Thanks again
Sayeed Dewan Ronju

[image: Mailtrack]

Sender
notified by
Mailtrack

12/29/19,
11:10:26 AM

Votes

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
community guidelines
Community Expert ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

The condition you need is an "And", so using the "OR" operator makes no sense. You may be experiencing problems with the "And" because of operator precedence, i.e, the "And" has greater precedence than the comparison operators.  Bracket the operations to fix this issue.

 

if ((event.value >= "90") && (event.value <= "130.00") )

 

I always bracket the operations so that they are both clear to the reader as well as the parser. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
New Here ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Sorry it is not working at all. It catches every number and giving alert.

Thanks
Sayeed Dewan Ronju

[image: Mailtrack]

Sender
notified by
Mailtrack

12/29/19,
12:57:23 PM

Votes

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
community guidelines
LEGEND ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

This looks bad to me:

if (event.value >= "90" || event.value <= "130.00" )

This test will be true for "90", and for "9".

This test will be true for "130.0" and for "13".

You need to compare numbers not strings. 

 

 

Votes

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
community guidelines
LEGEND ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

A tip: there are so many possibilities with programming that saying "it is still not working" without posting your new code is pointless. I want to see also that you have stopped comparing strings.

Votes

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
community guidelines
New Here ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Thanks for asking.
I am giving you whole matter.


I have multiple fields to enter data. I am trying to VALIDATE that with
results like ALERT and COLOR CHANGE in the same field where the data was
entered. It is working fine up to certain range. But when the goes within
the range of other field it is giving conflicting result.

Suppose I put the following in one field:

event.rc = true;
if (event.value >= "5" || event.value <= "10.0")
{
event.target.textColor = color.blue;
}
else
{
app.alert("The entered value is after lunch glucose and should be between
'5.0' to '10.0'");
event.target.textColor = color.red;
}





And in another field I put:

event.rc = true;

if (event.value >= "90" || event.value <= "130.00" )

{
event.target.textColor = color.blue;



Suggestion I have got:

The condition you need is an "And", so using the "OR" operator makes no
sense. You may be experiencing problems with the "And" because of operator
precedence, i.e, the "And" has greater precedence than the comparison
operators. Bracket the operations to fix this issue.



if ((event.value >= "90") && (event.value <= "130.00") )



I always bracket the operations so that they are both clear to the reader
as well as the parser.

I reply I said:

Sorry it is not working at all. It catches every number and giving alert.

Thanks
Sayeed Dewan Ronju


[image: Mailtrack]

Sender
notified by
Mailtrack

12/29/19,
01:23:47 PM

Votes

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
community guidelines
Community Expert ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Why does you compare the value with strings?

Votes

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
community guidelines
LEGEND ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Comparing strings is legal and valid, but probably not what you want to do. The string "30" is less than "200" and more than "4".

Votes

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
community guidelines
LEGEND ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

I have that wrong... actually the string "30" is MORE than "200" and LESS than "4". This is easy to get wrong!

 

if ( "30" > "200" ) app.alert("30 is more than 200") 

if ( "30" < "4" ) app.alert("30 is less than 4") 

Votes

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
community guidelines
Community Expert ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

That's because strings are compared one character at a time. So a string starting with "2" will always be "less" than a string starting with "3" (regardless of their length) even if the former is "200" and the latter is "30"...  That's why you need to not use quotes when comparing numbers, or they'll be treated as strings.

Votes

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
community guidelines
New Here ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

LATEST
Thank you very much. Quotes were the main problem. I used && in between the
conditions and removed quotes. It is working perfect. Thank you again for
helping me.
Sayeed Dewan Ronju

[image: Mailtrack]

Sender
notified by
Mailtrack

12/29/19,
04:54:30 PM

Votes

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
community guidelines
Community Expert ,
Dec 29, 2019 Dec 29, 2019

Copy link to clipboard

Copied

Drop the quotes around all numbers.

Votes

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
community guidelines