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
Copy link to clipboard
Copied
Why does you use || ?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Why does you compare the value with strings?
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".
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")
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Drop the quotes around all numbers.