Copy link to clipboard
Copied
Hope someone can help me with this. I think I understand how to do the individual action but can't seem to put them all togther to work.
I have 2 values ( A and B) that I need to multiply. This is easily done. I then need to validate that the answer does not exceed a max number (example 500). if it is over the max dose, the answer will always be 500. if the value is below 500, the number needs to be rounded to the nearest 50.
MODIFIED REPLY
Hi,
Assuming that you already have two form fields named A and B, you can try adding the following script to the custom calculation script of your total form field and see if it works for you:
//Add this script as custom calculation script of your total field
if(!event.willCommit)
console.println(!event.value);
var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 50;
var d = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b
...
Thank you for spotting that. My mistake.
This is the correction:
if(!event.willCommit)
console.println(!event.value);
var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b > c) event.value = c;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;
//Or you can use
if (a*b < c) event.value = Math.round((event.value
...
Another way of doing it is with the min method of the Math object, like so:
var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);
var v1 = (Math.round((a*b)/50))*50;
event.value = Math.min(v1, 500);
You didn't specify if you want to round the number down or up (or to the nearest) 50 multiple, so I used the latter.
If you want to round up replace Math.round with Math.ceil, and if you want to round down replace it with Math.floor in the third line
...Thanks everyone!
Here is what I came up with base on your replies. This is actually a medication calculation on a PDF form. When a checkbox is ticked off, the calculation occurs. Works perfectly!
if(this.getField("Rituximab").value=="Off")
{
event.value="";
}
else
{
var bsa_val = this.getField("BSA").value * 375
if(bsa_val > 800){
event.value = 800
}
else
{
event.value = Math.round(bsa_val / 50) * 50;
}
}
Copy link to clipboard
Copied
MODIFIED REPLY
Hi,
Assuming that you already have two form fields named A and B, you can try adding the following script to the custom calculation script of your total form field and see if it works for you:
//Add this script as custom calculation script of your total field
if(!event.willCommit)
console.println(!event.value);
var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 50;
var d = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b >500) event.value = d;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;
//Or you can use
if (a*b < c) event.value = Math.round((event.value/50))*50;
Copy link to clipboard
Copied
Why the variable c ?
Copy link to clipboard
Copied
Thank you for spotting that. My mistake.
This is the correction:
if(!event.willCommit)
console.println(!event.value);
var a = this.getField("A").value;
var b = this.getField("B").value;
var c = 500;
if (a && b !="") event.value = a*b;
event.value = a*b;
if ( (a =="") && (b > 1) ) event.value = "";
if ( (a>1) && (b =="") ) event.value = "";
if (a*b > c) event.value = c;
if (a*b < c) event.value = Math.ceil((event.value/50))*50;
//Or you can use
if (a*b < c) event.value = Math.round((event.value/50))*50;
Copy link to clipboard
Copied
Another way of doing it is with the min method of the Math object, like so:
var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);
var v1 = (Math.round((a*b)/50))*50;
event.value = Math.min(v1, 500);
You didn't specify if you want to round the number down or up (or to the nearest) 50 multiple, so I used the latter.
If you want to round up replace Math.round with Math.ceil, and if you want to round down replace it with Math.floor in the third line of code above.
Copy link to clipboard
Copied
Awesome! thanks.
Copy link to clipboard
Copied
Thanks everyone!
Here is what I came up with base on your replies. This is actually a medication calculation on a PDF form. When a checkbox is ticked off, the calculation occurs. Works perfectly!
if(this.getField("Rituximab").value=="Off")
{
event.value="";
}
else
{
var bsa_val = this.getField("BSA").value * 375
if(bsa_val > 800){
event.value = 800
}
else
{
event.value = Math.round(bsa_val / 50) * 50;
}
}