Skip to main content
Participant
December 29, 2019
Question

Data in multiple fields conflicting each other in javascript

  • December 29, 2019
  • 5 replies
  • 1391 views

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

This topic has been closed for replies.

5 replies

try67
Community Expert
Community Expert
December 29, 2019

Drop the quotes around all numbers.

Legend
December 29, 2019

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".

Legend
December 29, 2019

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

try67
Community Expert
Community Expert
December 29, 2019

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.

Legend
December 29, 2019

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.

Sayeed1Author
Participant
December 29, 2019
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
Bernd Alheit
Community Expert
Community Expert
December 29, 2019

Why does you compare the value with strings?

Legend
December 29, 2019

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. 

 

 

Bernd Alheit
Community Expert
Community Expert
December 29, 2019

Why does you use || ?

Sayeed1Author
Participant
December 29, 2019
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
Thom Parker
Community Expert
Community Expert
December 29, 2019

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 PDFScriptingUse the Acrobat JavaScript Reference early and often