I have a form created in html, with two input fields and one button. When clicking on the button i want a function to be called for that will then add the two numbers and then display the sum via the alert function.
I know how to call for the function when the button is clicked, and i know how to read and store the two inputs in separate variables.
I would like some tips on how to in the easiest way validate the input as numbers only?
function Calculate() {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
num1_parsed = parseInt(num1);
num2_parsed = parseInt(num2);
if (num1_parsed) {
} else {
alert("Wrong input!!!");
return false;
}
var total = num1_parsed + num2_parsed;
alert("The total sum of your numbers are: " + sum);
}
This code works for some reason, tho if num1 is correct but num2 is not it gives message NaN instead of the alert message!