//declare variables function calculate(){ var weight, height, bmi; weight= $('#weight').val(); height= $('#height').val();
//calculate total bmi = weight*703/(height*height); bmi = bmi.toFixed(1);
$('#bmi').val(bmi);
if (bmi < 18.5){ $('#bmi_message').html('Your BMI shows you are under weight.'); }
if (bmi >= 18.5 && bmi <= 24.9){ $('#bmi_message').html('Your BMI shows you are a normal weight.'); } if (bmi >= 25 && bmi <= 29.9){ $('#bmi_message').html('Your BMI shows you are over weight.'); }
if (bmi > 30){ $('#bmi_message').html('Your BMI show you are obese.'); }
return false;
}//end of calculate function
$('#bmiform').submit(calculate); $('#bmiform').validate({ rules: { }, messages:{ height: "Please enter a valid height between 24 in. and 100 in.", weight: "Please enter a valid weight between 20 lb. and 500 1b." }
}); return false;
});
I am trying to get this so that it does not calculate unless the form is validated. Not sure what I am doing wrong.
Form Validation
in Modern Javascript: Develop and Design
Posted
// JavaScript Document
$(document).ready(function(){
//declare variables
function calculate(){
var weight, height, bmi;
weight= $('#weight').val();
height= $('#height').val();
//calculate total
bmi = weight*703/(height*height);
bmi = bmi.toFixed(1);
$('#bmi').val(bmi);
if (bmi < 18.5){
$('#bmi_message').html('Your BMI shows you are under weight.');
}
if (bmi >= 18.5 && bmi <= 24.9){
$('#bmi_message').html('Your BMI shows you are a normal weight.');
}
if (bmi >= 25 && bmi <= 29.9){
$('#bmi_message').html('Your BMI shows you are over weight.');
}
if (bmi > 30){
$('#bmi_message').html('Your BMI show you are obese.');
}
return false;
}//end of calculate function
$('#bmiform').submit(calculate);
$('#bmiform').validate({
rules: {
},
messages:{
height: "Please enter a valid height between 24 in. and 100 in.", weight: "Please enter a valid weight between 20 lb. and 500 1b."
}
});
return false;
});
I am trying to get this so that it does not calculate unless the form is validated. Not sure what I am doing wrong.