2

I have this script to calculate a form

    $(".mad, .mad2, .mad3, .mad4, .mad5, .mad6").each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            antal += parseFloat(this.value);
             subtotal = antal*30;
             $(this).css("background-color", "#ffffff");
        }
        else if (this.value.length != 0){
             $(this).css("background-color", "#e89898");
       }
    });
        $("#antal").html(antal.toFixed(0));
        $("#subtotal").html(subtotal.toFixed(0));

it seems to work fine, if one is trying to ad a letter or its being removed and the input field turns red. however i dos accept space resulting in the antal and subtotal Show NaN

How do i get it to work for both letters AND space (not accepting space either ) ?

Jesper Møller
  • 137
  • 1
  • 1
  • 8

2 Answers2

1

Try and use jquery's trim() method to remove all the whitespaces:

if (this.value.trim().length != 0 && !isNaN(this.value.trim()) ) {

}
user1636130
  • 1,615
  • 5
  • 29
  • 47
0
if (parseFloat(this.value) == this.value) {
daghan
  • 948
  • 10
  • 18