Skip to main content
Known Participant
November 27, 2024
Question

BMI calculated or entered manually

  • November 27, 2024
  • 3 replies
  • 259 views

I have a BMI (body mass index) field that get calculated when the user enters the weight and height,:

weight / ((height/100)*(height/100));

 

But sometime we would like to just enter the BMI manually because we don't know their weight and height.

There is another layer of difficulty because the BMI is broken down into BMIwhole and BMIdecimal.

 

I put the following code on blur in the kg field::

var weight = this.getField("kg").value;
var height = this.getField("cm").value;


if (this.getField("BMIwhole").value == "")
{
if(weight != "" && height != ""){


calcul = weight / ((height/100)*(height/100));
calculDecimal = calcul;
partieEntiere = Math.floor(calcul);

if(partieEntiere<=0) {partieEntiere="";}
this.getField("BMIwhole").value = partieEntiere;
this.getField("BMIdec").value = Math.round(10*(calculDecimal-partieEntiere));


{if (this.getField("BMIdec").value == "10")
{
this.getField("BMIwhole").value = partieEntiere+1;
this.getField("BMIdec").value = "0"
}}
}} else {
this.getField("BMIwhole").value = "";
this.getField("BMIdec").value = "";
}
if (this.getField("BMIwhole").value >= "1");
{
event.rc = false;
}

But when I just enter the BMI manually, it will be deleted on the next keystroke.

 

3 replies

Participant
October 11, 2025

Your script clears the manual BMI because the if line that checks BMIwhole greater or equal to 1 ends with a semicolon, which makes the condition run every time and resets the fields. Remove that extra semicolon and make sure the calculation only runs when the BMI fields are empty so it does not overwrite manually entered values.

try67
Community Expert
Community Expert
November 27, 2024

In an On Blur event this line has no effect:

 

event.rc = false;
PDF Automation Station
Community Expert
Community Expert
November 27, 2024

You should have a custom calculation script in each field that you want a calculated result, not a blur event and event.rc doesn't need to be used.   This article will help with the custom calculation for BMI that also allows a manual entry.