Skip to main content
Participant
October 26, 2020
Question

Calculation validation script based on radio button and checkbox

  • October 26, 2020
  • 2 replies
  • 1481 views

I would like help with the following please:

 

  1.  Radio button group 'Soort' existing of options 'New' and 'Damaged'.
  2. When 'Damaged' is selected, 4 checkboxes appear: 'VZ', 'AZ', 'BZ', 'PZ'.
  3. When for instance 'VZ' is selected, checkboxes 'V1', 'V2', and 'V3' appear, each containing a different value. Same for 'AZ' ('A1', 'A2', ...) and the other ones.
  4. 'Total' is the field for the result of the calculation, based on the input.

 

When 'New' is selected the calculated value of 'Total' must be overwritten with value '795', otherwise the values of 'V1', 'V2', 'V3', 'A1', 'A2', and so on must be added up to 'Total'.

 

I currently have place this script to the 'Total' field (tab Calculate, alternate calulated script), but keep getting errors when 'Damaged' is selected (possibly because the underlying options are not selected yet?):

 

var soort = this.getField("Soort").valueAsString;

if (soort=="New") { event.value = "795"; }

else if (soort=="Damaged")

{ var v1=this.getField("V1").value;

var v2=this.getField("V2").value;

var v3=this.getField("V3").value;

var v4=this.getField("V4").value;            //and so on...

event.value = 0+v1+v2+v3+v4; }

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
October 26, 2020

What error messages does you get?

Participant
October 27, 2020
The entered value does not match the format of the field
try67
Community Expert
Community Expert
October 27, 2020

This is most likely because you're using a number format that isn't acceptable for a script. You mentioned "17,40". If you want to enter it like that then you have to use the "1.234,56" option under Number in the field's Format tab, so that it would know you're using the comma as the decimal separator. If you don't do that it won't be able to convert this value to a number.

Nesa Nurani
Community Expert
Community Expert
October 26, 2020

Create hidden text field, in that field go to calculate tab and use "value is the" and pick all of your checkboxes that you want to calculate( v1-v4, a1-a4...etc)
then in total field use this code:
var sort = this.getField("Soort").valueAsString;
var hid = Number(this.getField("Hiddenfield").valueAsString);

if(sort == "New"){
event.value = "795";}
else if(sort == "Damaged"){
event.value = hid;}

Participant
October 27, 2020

Almost... The error message have dissappeared 🙂

 

Unfortunately at selecting the checkboxes, the values aren't calculated at once. So I get 'delayed' feedback, and therefore incorrect values within the field 'Total'.

 

For instance: Value of checkbox 'V1' = 17,40. On selection the value of 'Total' doesn't change. On de-selection thereafter the value of 'Total' changes to 17,40 ...

Nesa Nurani
Community Expert
Community Expert
October 27, 2020

Go to field calculation order and make sure hidden field is above total field, that should fix delay.