Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Validation & Calculate BMI

This is a BMI calculator. I expect to check the data validation first. text fields cannot be empty. And then calculate the BMI and display into another text box.. Validation part is working properly, but the calculating function is not. Please help me to find the error.

function validate() {
  if (document.myForm.weight.value == "") {
    alert("Please provide your weight!");
    document.myForm.weight.focus();
    return false;
  }

  if (document.myForm.height.value == "") {
    alert("Please provide your heught!");
    document.myForm.height.focus();
    return false;
  }
  calBMI();
}

function calBMI() {
  var weight = getElementById("weight").value;
  var height = getElementById("height").value;
  var bmi = weight / (height * height);

  document.getElementById("bmi").innerHTML = bmi;
}
<body>
  <form name="myForm">

    <label>weight</label>
    <input type="text" name="weight" id="weight">

    <label>height</label>
    <input type="text" name="height" id="height">

    <input type="text" readonly="readonly" id="bmi">

    <input type="submit" value="Submit" onclick="validate() calBMI()">
  </form>

</body>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

function validate() {
  var height = document.getElementById("height").value;
  var weight = document.getElementById("weight").value;
  
  if (height == "" || height == 0) {
    document.getElementById("result").innerHTML = "Please enter a valid height";
    return;
  }
  
  if (weight == "" || weight == 0) {
    document.getElementById("result").innerHTML = "Please enter a valid weight";
    return;
  }
  calBMI();
}

function calBMI() {
  var weight = document.getElementById("weight").value;
  var height = document.getElementById("height").value;
  var bmi = weight / (height * height);

  document.getElementById("result").innerHTML = `BMI: ${bmi}`;
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
  <div>Weight</div>
  <input type="number" id="weight">
  <div>Height</div>
  <input type="number" id="height">
  <input type="submit" value="Submit" onclick="validate()">
  <div id="result"></div>
</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Add proper functions to calculate bmi. No need to call calculatebmi on submit.

var form = document.getElementsByName('myForm')[0];
form.addEventListener('submit', validate);

function validate(e) {
  e.preventDefault();
  if (document.myForm.weight.value == "") {
    alert("Please provide your weight!");
    document.myForm.weight.focus();
    return false;
  }

  if (document.myForm.height.value == "") {
    alert("Please provide your heught!");
    document.myForm.height.focus();
    return false;
  }
  var weight = document.myForm.weight.value;
  var height = document.myForm.height.value;
  calBMI(weight, height);
  return true;
}

function calBMI(w, h) {
  var bmi = Math.ceil((w / Math.pow(h, 2)) * 703);
  document.getElementById("bmi").setAttribute('value', bmi);
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>

  <form name="myForm">

    <label>weight</label>
    <input type="text" name="weight" id="weight">

    <label>height</label>
    <input type="text" name="height" id="height">

    <input type="text" readonly="readonly" id="bmi">

    <input type="submit" value="Submit" onclick="validate();">
  </form>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda