1

I have a jsFiddle to demonstrate my issue (and allow you guys to straighten me out).

I'm simply checking the values of two input textboxes and alerting the user if the max price is less than the minimum price, but they're evaluating backwards! I have if (maxValue < minValue)... but it evaluates it as if the operator is "is greater than".

What am I missing?!?

<form id="search" action="search.php" method="post">

    <label id="lblPriceMin" for="txtPriceMin">Minimum Price</label>
    <input type="text" id="txtPriceMin" name="priceMin"></input>
    <br />
    <br />

    <label id="lblPriceMax" for="txtPriceMax">Maximum Price</label>
    <input type="text" id="txtPriceMax" name="priceMax"></input>
    <br />
    <br />

    <input type="reset" id="reset" name="reset" value="Clear Form" />
</form>

Here's the js,

$('#txtPriceMax').focusout(function() {

    var minValue = $('input#txtPriceMin').val();
    var maxValue = $('input#txtPriceMax').val();

    //alert ('minValue: ' + minValue + ', maxValue: ' + maxValue);

    if (maxValue < minValue) {
        alert ('The maximum value (' + maxValue + ') must be greater than the minimum value (' + minValue + ')!');
    }
});
1

1 Answer 1

8

Use parseFloat()

jsFiddle Example

if (parseFloat(maxValue) < parseFloat(minValue)) 
Sign up to request clarification or add additional context in comments.

2 Comments

Ah! So I was comparing strings! Thanks, Gabe!
@eventide You bet, glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.