Skip to main content
Participant
Oct 26, 2020 3:08 PM UTC
Question

Calculation validation script based on radio button and checkbox

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 is closed to new replies. Start a new post to keep the conversation going.

10 replies

Bernd Alheit
Community Expert
Community Expert
Oct 26, 2020 4:25 PM UTC

What error messages does you get?

Translate
Participant
Oct 27, 2020 8:52 AM UTC
The entered value does not match the format of the field
Translate
try67
Community Expert
Community Expert
Oct 27, 2020 9:29 AM UTC

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.

Translate
Nesa Nurani
Community Expert
Community Expert
Oct 26, 2020 4:14 PM UTC

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;}

Translate
Participant
Oct 27, 2020 9:02 AM UTC

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 ...

Translate
Nesa Nurani
Community Expert
Community Expert
Oct 27, 2020 9:50 AM UTC

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

Translate