How to validate totals on a form before submission

  • Get Answers for topics that you need help with
    Noob here - thanks for any help… including RTFM with a link …

I have a form where I’m asking for three values: a total and two sub-totals that ought to add up to the total. I have a calculated field that does the addition of the subtotals. I am struggling to figure out how to validate that the calculated field equals the manually entered total.

This is a simplified version of what I’m ultimately trying to do.

Thanks again!
John

Hi John,

If you add a multi-choice field called something like “Balanced” with two options “Yes/No” and then set two conditional rules on it.
The rule will be based on a field value.

https://learn.knack.com/article/oaj8ajhwkc-conditional-rules#adding_conditional_rules

The first states that if the total equals the calculated sub totals then the value is”Yes”.
The other is the opposite, so equals “No” if the total does not equal the calculated sub totals.
You can then either show the “Yes/No” field in the table or just use a display rule on one of the values to turn the field red or green (or show an icon etc) based on whether the multi choice field is “Yes”’or “No”. This will give the user a visual clue that there is an issue.

You could then run a task, if you are on a Pro account or above, to look for any values that are “Yes” and send an email to notify on a daily schedule.

If you are trying to validate that the two sub totals equal the manually added total before the form is submitted this will require the skills of a JavaScript person to create a query before the record is added……that’s not me :stuck_out_tongue_closed_eyes:

Hope this helps.

1 Like

Thank you Carl. It did help just as you directed.
Took me a minute to figure out that the display rules are applicable once the form is submitted and the data is being displayed in a table (or some other view, I suppose).
I will now attempt JS.
Much appreciated!

1 Like

Hi John

Try something like this:

$(document).on(‘knack-view-render.view_308’, function(event, view, data) {
$("#view_308 .kn-button").on(“click”, function() {
if (Number($(‘input#field_372’).val()) != (Number($(‘input#field_370’).val()) + Number($(‘input#field_371’).val()))) {
alert (“Values Don’t Add Up!”);
return false;
}
})
});

2 Likes

@JulianKirkness - That works like a dream :blush:
Just be aware that when I copied the JavaScript into the editor I got red errors on the speech marks. You will need to replace any curly quotes with ’ and ".
It took me a moment to work out the calculation based on my fields, hopefully the attached will further illustrate. :pray:

1 Like