Felix
Felix

Reputation: 2661

JS if greater than condition not returning correct result

I'm testing some client-side validation on my site and I've run into a bit of a snag.

var budget = document.forms["jobSubmit"]["currency-field"].value; 
var price = budget.replace("$", "");


if (Number(price) >= 9999) {
    alert("Budgets have a maximum of 9999 US dollars.");
    return false;
} else {
alert(price);   
}

return false;

When I try to do a simple greater than condition on my price field, it doesn't return the proper result.

So in this situation, despite that I put in 11111, the greater than isn't working.

enter image description here

However, if I were to switch it to < 10, for example, it'll work just fine and all numbers under 10 will trigger the condition.

Why is this happening?

Upvotes: 0

Views: 91

Answers (1)

Mamun
Mamun

Reputation: 68933

Try the following:

var budget = "$111,111" 
var price = budget.replace("$", "").replace(',','');


if (Number(price) >= 9999) {
    alert("Budgets have a maximum of 9999 US dollars.");
    //return false;
} else {
  alert(price);   
}

Upvotes: 1

Related Questions