JeffVader
JeffVader

Reputation: 702

Jquery IF Statement Less Than issue

This should be simple but I seem to be missing something..

start = $('[name=start]').val().split('.');
last = start[3];
end = $('[name=end]').val();

if (end < last) {
   alert('BLAH BLAH');
   return false;
}

I've alerted the values out and for one test I could see

last = 100

end = 90

So if ( 90 < 100 ) alert should be shown but isn't.

What am I doing wrong ? Thanks

Upvotes: 0

Views: 65

Answers (3)

Dinesh
Dinesh

Reputation: 1007

Try Like This

    if(parseInt(end) < parseInt(last)){

           //your code
    }

Upvotes: 0

Rajesh Dhiman
Rajesh Dhiman

Reputation: 1896

Try this

if (parseInt(end,10) < parseInt(last,10)) { alert('BLAH BLAH'); return false; }

Upvotes: 0

Jon
Jon

Reputation: 437376

The values you are comparing are strings, not integers, so they are compared lexicographically. Try this in your browser console:

90 < 100       => true
"90" < "100"   => false

Convert them to numbers first to make a numeric comparison:

last = +start[3];   // The + prefix is equivalent to Number(start[3])
end = +$('[name=end]').val();

Note that the +/Number(...) approach will return NaN if either input is not a valid number; alternatively you could use parseInt which has somewhat different semantics.

Upvotes: 5

Related Questions