Jump to content
  • 0

Data validation for calculations


PaulBI
 Share

Question

Good day,

Does anyone know how to enforce field rules. On a submission form I have three fields FD_1, FD_2 and FD_3 and I want to make sure that when I add the fields together the result is not greater than 100.

Basically (FD_1 + FD_2 + FD_3 <= 100)

Thank you 
 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hi,

You can achieve this with a bit of JavaScript.

 

1) On your Submission Form DataPage, go to Configure Fields, add a Header & Footer.

2) Select the Footer, disable the HTML editor and paste the following:

 

<script>

document.addEventListener("BeforeFormSubmit", function(e){
var sum= parseInt(document.getElementById("InsertRecordFD_1").value) + parseInt(document.getElementById("InsertRecordFD_2").value) + parseInt(document.getElementById("InsertRecordFD_3").value);

 if(sum> 100)
{
e.preventDefault();
alert("Sum of fields should not exceed 100");
}

});

</script>

 

How this works is you are prevented from submitting the entry if the sum of the three fields exceed 100.

 

Link to comment
Share on other sites

  • 0

Hi, 

May I know where are the results displayed?

I encountered this one before and I am using 4 fields (1 field for sum using Calculated Value element). Those fields are Number data type. I used a CASE WHEN function on my Calculated Value element. Here is the formula I used: 

CASE WHEN ([@field:X] + [@field:Y] + [@field:Z]) <= 100

THEN ([@field:X] + [@field:Y] + [@field:Z])

ELSE 

0

END


When the sum exceeds 100, the current value of sum will become 0. I always rely to this Caspio Function Reference when I need to review some functions: https://howto.caspio.com/function-reference/

Link to comment
Share on other sites

  • 0

Thanks to both of you for taking the time to address my question.  Futurist your script works the way I needed it to so thank you. 

trickson I hadn't heard of CASE WHEN before but now seeing what it can do you have helped me solve another problem I was having difficulty with.

Thanks to both of you again.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...