1

I want to compare the two input values, just try in javascript, but it's not working fine. I'm using the following code

function check_closing()
{
var opening = $('#opening').val();
var closing = $('#closing').val();
if(opening > closing)
{
    alert('Opening is greater than Closing. Please enter the correct value');
    $('#closing').val('');
}
}

if the opening value input = 8541, closing value like = 8241 it's work fine, but if the closing is 954 it's not working. Please help.

Thanks in advance.

4 Answers 4

4

You're comparing the strings instead of integers, so you need to convert the strings to integers. Since as strings, '8541' > '8241'

>>>'8541' > '8241'
true
>>>'954' > '8241'
true

>>>8541 > 8241
true
>>>954 > 8241
false

So you want:

function check_closing()
{
    var opening = parseInt($('#opening').val());
    var closing = parseInt($('#closing').val());
    if(opening > closing)
    {
        alert('Opening is greater than Closing. Please enter the correct value');
        $('#closing').val('');
    }
}

To add more to why exactly this happened, in case you're interested: Strings are compared character by character, iirc. So the '9' is greater than the '8' but the '8241' is less than '8541' because '2' is less than '5'.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean '954' > '8241'
"Since as strings, '8541' > '8241'..." As numbers, too.
Yeah, whoops. Updating now. Both the comments meant that. I didn't get it originally. My bad.
2

The values of input elements are always strings. To compare them as numbers, you must convert them to numbers. You can do that with:

  • parseInt if they're whole numbers and you want to specify the number base and stop parsing at the first non-numeric character

    Example:

    var opening = parseInt($('#opening').val(), 10);
    // This is the number base, usually 10 ---^^^^
    
  • parseFloat if they're decimal fractional numbers

    Example:

    var opening = parseFloat($('#opening').val()); // Always base 10
    
  • The + if you want JavaScript to guess the number base, and give you NaN if there's a non-numeric character

    Example:

    var opening = +$('#opening').val();
    //            ^---- it's hiding here
    

...and a few others.

Comments

1

First of all you you need to convert the strings into integers

function check_closing(){
    var opening = parseInt($('#opening').val(), 10);
    var closing = parseInt($('#closing').val(), 10);
    if(opening > closing){
        alert('Opening is greater than Closing. Please enter the correct value');
        $('#closing').val('');
    }
}

Comments

0

Convert the strings to integers before comparison:

function check_closing()
{
    var opening = $('#opening').val();
    var closing = $('#closing').val();
    if(parseInt(opening) > parseInt(closing))
    {
        alert('Opening is greater than Closing. Please enter the correct value');
        $('#closing').val('');
    }
}

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.