I’m trying to check a value from two text fields with JQuery. I need to compare the dollar amounts to see if the new entered value is greater than the original amount. The outcome is not what I’m expecting at all. The first value is pre populated via PHP OrigAMTVal and the second value amtTOrefundValis entered by the user. Here’s the code:
$('#doREFUND').on("click", function() {
var amtTOrefundVal = $('.amtTOrefund').val();
var OrigAMTVal = $('.CustomerOrderTotal').val();
var doamtTOrefund = parseInt(amtTOrefundVal).toFixed(2);
var doOrigAMT = parseInt(OrigAMTVal).toFixed(2);
if( doamtTOrefund > doOrigAMT ) {
var balance = OrigAMTVal - doamtTOrefund;
alert("BAD: $" + balance + " Over Limit");
} else {
var balance = OrigAMTVal - doamtTOrefund;
alert("GOOD: $" + balance + " Left to Go");
}
});
Here’s a fiddle:
http://jsfiddle.net/dM2Fr/2/
I know I’m missing something, but I can’t figure it out.
toFixedreturns a string, just compare the values without that.The problem is you are comparing strings and
"174 > "1000"is true.P.S. You should use
parseInt(value, 10).