2

I have the 2 numbers that I want to compare entertedAmount = 65.00 and maxPrice = 5000.00 in my JavaScript Function.

var entertedAmount = parseFloat(textbox.value).toFixed(2);
        var maxPrice = parseFloat(passedMaxPrice).toFixed(2);

        if (entertedAmount <= maxPrice) {
                var strSplit = textbox.id;
                var res = strSplit.split("_");
                var rownumber = res[2].substring(res[2].length, 3) - 1;
                arrRows[rownumber] = removeSpaces(textbox.value);
                sumArray();
                isSearchButtonPressed = false;
        } else {
            alert('Please enter a price less than R' + passedMaxPrice);
            textbox.value = 0;
        }

if the enteredAmount is less than maxPrice it must enter the if statement but now it is not entering the if statement , it is going to ELSE statement

ellipsis
  • 12,049
  • 2
  • 17
  • 33

3 Answers3

2

The problem comes from your Tofixed function which converts the decimal obtained with parseFloat into a string. So you have to surround it with Number ().

var textbox = document.getElementById('textbox');
var entertedAmount = Number(parseFloat(textbox.value).toFixed(2));
var maxPrice = Number(parseFloat(5000).toFixed(2));


if (entertedAmount <= maxPrice) {
  console.log(entertedAmount + " less than "+ maxPrice );
    } else {
    alert('Please enter a price less than R');
        textbox.value = 0;
  }
<input id="textbox" type="number" value="522.5886"/>
soumare
  • 394
  • 5
  • 9
0
if(Number(entertedAmount) <= Number(maxPrice)){

}else{

}
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

toFixed returns a string so you have to parse that string into float

 if(parseFloat(entertedAmount) <= parseFloat(maxPrice)){

 } else{

    }

Hope this works for you.

Raihan Ridoy
  • 678
  • 8
  • 18