检查输入是否为所有数字html javascript
我需要确保用户输入的是所有数字,而不是体重、身高和年龄的字母检查输入是否为所有数字html javascript,javascript,html,Javascript,Html,我需要确保用户输入的是所有数字,而不是体重、身高和年龄的字母 function calculate(){ var w= document.getElementById("weight").value; var h=document.getElementById("height").value; var a=document.getElementById("age").value; //if all numbers enter lo
function calculate(){
var w= document.getElementById("weight").value;
var h=document.getElementById("height").value;
var a=document.getElementById("age").value;
//if all numbers enter loop
var height= Number(h);
if(document.one.gender[0].checked)
{var ans= 50+ (height/2) ;}
else
{var ans=46+ (height/2) ;}
alert(ans);
}
// else alert("enter numbers only");
您可以通过以下方式进行检查:
if (isNaN(document.getElementById("weight").value)){
// Not number
} else {
// It is number
}
编辑代码:
function calculate(){
var w= document.getElementById("weight").value;
var h=document.getElementById("height").value;
var a=document.getElementById("age").value;
if (isNaN(w) || isNaN(h) || isNaN(a)) {
alert("Please ensure that weight, height and age are integers");
return;
}
//Your code if all numbers
}
您是否尝试过使用isNaN()?欢迎使用!请你投票并接受我下面的回答好吗?:)永远欢迎!你能投票接受我的回答吗?:)