If you want to use a function and be able specify the unit of measurements:
// document level funciotn
function BMI (nMass, nHeight, cUnits) {
// set default units for calculation - English or Metric
if(typeof cUnits == "undefined") cUnits = "English";
// mass pounds, height inches
var nBMI = 0;
// perform calculation if we have valid data
if(nMass != "" && nHeight != "") {
// square the height
var nHeight2 = Math.pow(nHeight, 2);
// compute the division
nBMI = nMass /nHeight2
switch (cUnits.toLowerCase()) {
case "english":
// aplly conversion to engilish units
nBMI = nBMI * 703.06957964;
break;
case "metric":
// no adjustment needed
break;
default:
nBMI = 0;
break;
} // end switch units
} // end if data not null
// return computed BMI
return nBMI;
} // end BMI funciton
Custom JavaScript for BMI field:
// name for fields
var cMass = "Mass";
var cHeight = "Height";
// calculaiton assumes height is in inches
// calculaiton assumes mass is in pounds
// get the values of the fields
var mass = this.getField("Mass").value;
var height = this.getField("Height").value;
// get the values of the fields
var mass = this.getField("Mass").value;
var height = this.getField("Height").value;
event.value = BMI(mass, height, "English");
You can remove the "English" parameter if you want to. If you change "English" to "Metric" the computation will use metric units of the height in meters and weight in kilograms.