www.webdeveloper.com
Results 1 to 6 of 6

Thread: Help with Comparison Operators ???

  1. #1
    Join Date
    Mar 2006
    Posts
    7

    Post 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;
    }
    alert("Value good")

    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.

    Thanks in advance
    Chris

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    var MinValue = 935;
    var inputvalue = 1000;
    if (inputvalue <= MinValue )
    {
    alert("input value less than Min value")
    return false;
    }
    else {
    alert("Value good");
    return true;
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Dec 2004
    Posts
    8,637
    Quote 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;
        }
        alert("Value good") 
        return true;
    }
    Last edited by phpnovice; 03-07-2006 at 03:05 PM.

  4. #4
    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.

    Thanks in advance
    Chris

  5. #5
    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. #6
    Join Date
    Mar 2006
    Posts
    7
    Thanks so much, your a star. That did the trick for me.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles