# Thread: Help with Comparison Operators ???

1. Registered User
Join Date
Mar 2006
Posts
7

## Help with Comparison Operators ???

Hi all,

I'm using code in a javascript function to do a comparison between 2 numeric values.

Here is my code:

var MinValue = 935;
var inputvalue = 1000;
if (inputvalue <= MinValue )
{
alert("input value less than Min value")
return false;
}

The problem I'm having is this function is returning TRUE when the Input value is 1000 or greater.

Any help would be very much appreciated.

Chris

2. Code:
```var MinValue = 935;
var inputvalue = 1000;
if (inputvalue <= MinValue )
{
alert("input value less than Min value")
return false;
}
else {
return true;
}```

3. Banned
Join Date
Dec 2004
Posts
8,637
Originally Posted by ChrisC
The problem I'm having is this function is returning TRUE when the Input value is 1000 or greater.
Why is that a problem? Seems that is the way you've coded it. ...or, as the example above implies, did you mean to say that the function is not returning a true value? Also, rather than coding an else, just do this:
Code:
```function testit()
{
var MinValue = 935;
var inputvalue = 1000;
if (inputvalue <= MinValue )
{
alert("input value less than Min value")
return false;
}
return true;
}```
Last edited by phpnovice; 03-07-2006 at 03:05 PM.

4. Registered User
Join Date
Mar 2006
Posts
7
Sorry Folks,

What I should have said is, when the inputValue is greater than or equal to 1000 the the function goes into the alert("Value less than Min Value") section of the code.

This should have returned alert("Value good") as 1000 is not less than or equal to 935.

If the inputvalue is equal to 980 or 970 etc the function returns alert("Value good"), which is correct.
I cannot figure out why when the inputvalue is >= 1000 the function returns alert("Value less than Min Value").

The function is just not validating correctly.
The inputvalue is being read from a webpage and the MinValue is being read form the database.

Any suggestions would be much appreciated.

Chris

5. S.P.Q.R.
Join Date
Mar 2005
Posts
767
Sounds like you are comparing strings, not numbers. 935 < 1000 but "935" > "1000". Try if (+inputvalue <= +MinValue ) instead.

6. Registered User
Join Date
Mar 2006
Posts
7
Thanks so much, your a star. That did the trick for me.