Reputation: 6136
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
Reputation: 36784
There are multiple issues in your code:
.onclick
on submitBtn
, which is undefined..isNan()
which should be .isNaN()
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");
}
}
Upvotes: 3
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