Samuel
Samuel

Reputation: 6136

Js conditional statement not working

My if statement doesn't appear to be working when checking for nonnumberic inputs on the weight variable, upon submitting. Why is this?

   submitBtn.onclick = function(){

    var name = document.getElementById('name').value;
    var weight = document.getElementById('weight').value;
    var pound = weight * 2.20462;

    //Check that the value is a number
    if(isNan(weight)){
     alert("Please enter a number);
    }
   }

Here it is in JsFiddle link

Upvotes: 0

Views: 39

Answers (2)

George
George

Reputation: 36784

There are multiple issues in your code:

  • You attempt to call .onclick on submitBtn, which is undefined.
  • You attempt to call .isNan() which should be .isNaN()
  • You don't close the string passed to your alert() function:
//Define submitBtn
var submitBtn = document.getElementById('submitBtn');
submitBtn.onclick = function(){

    var name = document.getElementById('name').value;
    var weight = document.getElementById('weight').value;
    var pound = weight * 2.20462;

    //Call isNaN()
    if(isNaN(weight)){

        //Close your string
        alert("Please enter a number");
    }
}

JSFiddle

Upvotes: 3

Michael Schneider
Michael Schneider

Reputation: 506

You might should use isNaN

submitBtn.onclick = function(){

    var name = document.getElementById('name').value;
    var weight = document.getElementById('weight').value;
    var pound = weight * 2.20462;

    //Check that the value is a number
    if(isNaN(weight)){
       alert("Please enter a number");
    }
}

Upvotes: 1

Related Questions