First, your HTML is poorly formed. O <meta charset="utf-8"> should stay inside the <head>. The <center> and the <font face = "Corbel"> should stay inside the <body>. The <body> should not be inside the <head>. The order is thus:
<html>
<head>
<meta charset="utf-8">
<title>Testando JS</title>
<!-- Outras tags aqui se você quiser. -->
</head>
<body>
<!-- O conteúdo da página vem aqui. -->
</body>
</html>
Second, the tags <center> and <font> are considered obsolete 20 years ago! Since 1997, when HTML 4 came out. They should no longer be used. HTML 5 no longer accepts them (although the browser can still accept them for compatibility reasons). Use CSS instead.
Third, in its function teste, the document.peso and document.altura do not exist. Use the document.getElementById("peso") that you are using in the other function.
Fourth, within the function teste, the variables peso and altura are not defined. The fact that they are defined within calculaIMC makes them exist only within calculaIMC.
Fifth, do not use two functions within the onclick.
Sixth, use <input type="button"> costs to be much easier to work than <inpu type="submit">. You can give the submit via javascript.
Here’s your revised code, click the blue button Execute down there to test.
function calculaIMC(peso, altura) {
return Math.round(peso / (altura * altura));
}
function teste() {
var elementoResultado = document.getElementById("resultado");
var elementoPeso = document.getElementById("peso");
var elementoAltura = document.getElementById("altura");
var peso = parseFloat(elementoPeso.value);
var altura = parseFloat(elementoAltura.value);
if (isNaN(peso) || peso <= 0.0) {
elementoResultado.innerHTML = "Preencha o valor do peso com um número.";
elementoPeso.focus();
return;
}
if (isNaN(altura) || altura <= 0.0) {
elementoResultado.innerHTML = "Preencha o valor da altura com um número.";
elementoAltura.focus();
return;
}
var imc = calculaIMC(peso, altura);
elementoResultado.innerHTML = "Seu IMC é " + imc + ".";
//document.getElementById("formulario").submit();
}
<form id="formulario">
<div>
Digite seu peso (Kg):
<input type="text" name="peso" id="peso" value="xx" maxlength="3" size="10" style="text-align: center">
</div>
<div>
Digite sua altura (m):
<input type="text" name="altura" id="altura" value="x.xx" maxlength="4" size="10" style="text-align: center">
</div>
<div id="resultado"></div>
<input type="button" value="Calcular IMC" onclick="javascript:teste()">
</form>
Ah, look at that code //document.getElementById("formulario").submit();? So if you take the // it will make the form Submit, but only if the weight and height are valid.
Thanks so much for the friendly answer!
– Allan Lima