Copy link to clipboard
Copied
I am trying to get a form text box to show the result of a calculation. I have 3 text boxes. Each one has a user entered numeric value. A fourth text box will show the value of the three box calculation. the result will show the form users BMI (body mass index). the formula is (Weight (lb) / (Height (in))2) x 703/(lb/in2)
Form field for weight is named lb; height filed is named ft; inch is named in.
Any idea how to write this in the field validate so that the result shows up in the "BMI" field? Thanks
The following more extensive script will report any field access errors and round the field's stored result to 1 decimal place.
function GetField(oDoc,cName)
{
// purpose get field objec and report if error occurs;
// return field object from document object for field name;
var oField = oDoc.getField(cName);
if(oField == null)
{
app.alert("Error accessing field named " + cName, 1, 0);
}
return oField;
} // end GetField function;
// custom JavaScript calculation for CalcBMI field;
var oHeightFt = Get
Copy link to clipboard
Copied
Why not just use the compute tab for the BMI field?
Since you have a divisor that will have a zero value you need to use an "if" statement to prevent division by zero or a null value.
You will need to use the "Custom JavaScript" calculation option. A possible script could be:
var nWeight = this.getField("Weight").value; // weight in lbs;
var nHeight = this.getField("Height").value; // height in inches;
event.value = "";
if(nHeight != 0)
{
// BMI = ( Weight / Height^2) / 703;
event.value = (nWeight / Math.pow(nHeight,2)) * 703;
}
Copy link to clipboard
Copied
Hi. Tried the script but nothing showed up in the calculated BMI field. Thanks for your input though.
Here's a screenshot of the fields in that form. I'm placing the formula in the "Calculated BMI" field.
Thanks again.
Copy link to clipboard
Copied
You need to enter a value into one of the fields for it to "kick in", and if nothing happens (or you're getting the wrong result), press Ctrl+J and check the window that opens for error messages.
Copy link to clipboard
Copied
You are now showing the height as feet and inches but only described the height as inches. I assume you know how to convert feet and inches to inches.
Copy link to clipboard
Copied
Yep, know how to convert ft to inches. However, the user has to enter height in one field then inches in the second field. So the formula I've used is height (feet) value times 12 plus the inches value which gives me the value for total height in inches. BTW, after each time i change the script in the "custom calculation script" tab field I click on preview, add values to the fields and nothing shows up in the BMI field.
I have used the same formula in Excel and it works every time. Obviously scripting in acrobat is different than excel, but I don't know javascript.
Copy link to clipboard
Copied
Check the calculation order.
Copy link to clipboard
Copied
Thanks, Bernd. I did that but still no success.
Copy link to clipboard
Copied
Can you share a link of the form?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Bernd, this may help... here's the formula from the excel spreadsheet i'm trying to duplicate in the form field.
=((E9*703)/((A9*12)+C9))/((A9*12)+C9)
where E9=weight in lbs, A9=feet, C9=inches
Copy link to clipboard
Copied
The following more extensive script will report any field access errors and round the field's stored result to 1 decimal place.
function GetField(oDoc,cName)
{
// purpose get field objec and report if error occurs;
// return field object from document object for field name;
var oField = oDoc.getField(cName);
if(oField == null)
{
app.alert("Error accessing field named " + cName, 1, 0);
}
return oField;
} // end GetField function;
// custom JavaScript calculation for CalcBMI field;
var oHeightFt = GetField(this, "ft"); // height in feet;
var oHeightIn = GetField(this,"in"); // height in inches above feet value;
var oWeight = GetField(this, "lb"); // weight in lbs;
event.value = ""; // clear field;
// compute if input fields not null;
if(oHeightFt != null && oHeightIn != null && oWeight != null)
{
var nHeight = (oHeightFt.value * 12) + oHeightIn.value; // convert height in feet and inches to height in inches only;
// calculate BMI if height not zero;
if(nHeight != 0)
{
var nBMI = (oWeight.value / Math.pow(nHeight, 2)) * 703; // compute BMI
event.value = Number(util.printf("%,101.1f", nBMI)); // round resutl to 1 decimal palce;
} // end height not zero;
} // if inputs field not null;
// end custom calculation script for CalcBNI field;
Copy link to clipboard
Copied
Awesome. Your script worked perfectly. Thank you so much for the help, really appreciate the time you spent doing this. Hope all is well with you and yours.
Copy link to clipboard
Copied
There is no calculation in the form.
Copy link to clipboard
Copied
See Compute BMI with comments for an example with working code and comments about how to better make use of form fields and their properties. I have also included an Excel file that shows how to suppress the divide by zero error in Excel. Unlike Excel PDF forms stop functioning when a divide by zero event occurs.
Copy link to clipboard
Copied
Hi guys, I am new to Adobe form and java etc.
I tried to use this formula but I have little bit different situation as I would like to create BMI calculator but based on height in cm (not innches/feet) are you able to help me with this please as I tried to change some parameters myself but still not successful 😞
BMI in the red circle should be 26.4 but it is showing different value 😞 - photo attached
Your help will be apprieciated :)))
Thank you in advance.
Luke
Copy link to clipboard
Copied
What script does you use?
Copy link to clipboard
Copied
Hi Bernd. thank you for stooping by 🙂
I think : Custom calculation script will be the best for this?
Copy link to clipboard
Copied
Yes.
Copy link to clipboard
Copied
any chance you can help me with this script, how to change formula for weight / (height in cm )2 ?