Trying To Get Multiple Number Fields To Sum Up and Then Match A Single Number Field

I’m trying to figure out how to get multiple number fields (3 of them) have their values summed and then have that summed field match a different single number field. If it matches, then the user can proceed and submit the form. If the two fields to not match, then the user will get an error message saying that the 3 fields summed up do not equal the other 1 field.

So far I have it set to

Number 1 Field Quantity:
Number 2 Field Quantity:
Number 3 Field Quantity:
Number 4 Field - Total Quantity of 1 through 3 (Hidden):

Number 5 Field - Quantity MUST Match Number 4 Field. If it does, user can proceed with the form. If it doesn’t, user cannot submit the form until the Number 1, 2, 3 or 5 number fields are adjusted so that #4 matches #5.

I’ve tried searching for a solution but can’t find anything to do this exact thing. Any advice?

Thank you!

Might you try adding a Number 6 Field (hidden) that would calculate field 5 minus field 4? Using a conditional check for the Submit button in settings (or a conditional check on any further sections), you can see whether Field 6 equals zero. If it does equal zero (i.e. Field 4 equals Field 5), then show the Submit button.

1 Like

That’s a good idea and another way to make it work! I went ahead and added a filter to the functions.php file to make this happen. I’m sharing a simple version of the code just in case it helps someone:

add_filter( 'gform_field_validation_1_5', function ( $result, $value, $form, $field ) {
$number5field = rgpost( 'input_5' );
$number4field = rgpost( 'input_4' );
if ( $result['is_valid'] && $number5field != $number4field ) {
$result['is_valid'] = false;
$result['message'] = 'Your error message here';
}
return $result;
}, 10, 4 );
2 Likes

Thank you for sharing the code!