0

Am comparing 2 values in text box

say $('#Low'.val()) > $('#High'.val()) //Where low and high are textbox id

But it is fails in few scenarios as it is comparing string.

i have 2 questions here

1.how to convert to integer or float and check it in correct way

2.When i compare the string as per my above code "99" > "1000" is returning true. Why it succeeds ?

Thanks

0

5 Answers 5

3
parseInt($('#Low'.val(),10)) > parseInt($('#High'.val(),10))

or

 parseFloat($('#Low'.val())) > parseFloat($('#High'.val())
Sign up to request clarification or add additional context in comments.

1 Comment

And "parseFloat" similarly
1

You have used function as $('#Low'.val()) which is wrong. It should be $('#Low').val()

Do this way:-

parseInt($('#Low').val(), 10) > parseInt($('#High').val(), 10)

Comments

1

Try This:

(parseInt($("#Low").val()) > parseInt($('#High').val()))

Comments

1

Even better is to use parseFloat.

parseFloat($('#Low').val())) > parseFloat($('#High').val())

Comments

0

Alphabetically 9 comes after 1. So "99" > "1000" may return true.

if(parseInt($('#Low'.val())) > parseInt($('#High'.val()))
{
    //Yes, #Low is bigger than #High
}
else
{
    //No, #Low is not bigger than #High yet.
}

must work.

Comments

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.