0

My submit click function is as below. aAmt is a $ field like for eg. $45.00 a_amount is always 10000. I am converting a_amount to $ in displayCurrencyFormat function.

I am then converting both to parseInt and doing >= comaprison and it fails. Even though aAmt is > $10000 and conition should display alert it doesnt.

$("#submitId").click(function () {      
var aAmt = $("#aAmt").val();
var a_amount = "${dAmt}";       

a_amount = displayCurrencyFormat(a_amount);     
var pLen = $("#pOd").val();



if ((parseInt(aAmt) >= parseInt(a_amount)) && (pLen.length == 0)) {
    $('#pDiv').text('Please provide a password');   
    $("#pOd").focus();  
    return false;
}

...//

});


function displayCurrencyFormat(a_amount)
 {
    //convert amount to currency format
    var nbrAmt = Number(a_amount.replace(/[^0-9\.]+/g,""));
    var fmtAmt = '$' + nbrAmt.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");    
    return fmtAmt;
 }
Geek
  • 3,187
  • 15
  • 70
  • 115

2 Answers2

1

Do you know that parseInt('$10000') actually giving you "NaN"? And as i can see here you are trying to compare integer and string...

Just try to alert aAmt and a_amount variables before you compare them and you will see what is actually going on...

Aleksandar Đokić
  • 2,118
  • 17
  • 35
1

You should convert it into an integer-like data first, and then you can compare them.

The currency formatted input are considered as not a number format so comparing them are something like String to String comparison, not Number to Number like what you want to achieve.

Refrence link: How to convert a currency string to a double with jQuery or Javascript?

Community
  • 1
  • 1
Chris Qiang
  • 223
  • 4
  • 19