Home › Forums › Quform WordPress › Minimum and maximum selected checkboxes
- This topic has 8 replies, 2 voices, and was last updated 1 month ago by
Abbas.
- AuthorPosts
- March 2, 2025 at 11:33 pm #37682
Abbas
ParticipantHi,
I have a form with more then 10 checkboxes
When the submit button is pressed I want to check the number of checkboxes that have been selected so that it should be 4 minimum, 6 maximum.
AND
Is it possible to check the sum of the values in the drop-down lists (Goal importance ratio) so that the sum should not be more than or less than 100?
In the attachments is a picture that may clarify the idea, in addition to that I exported the form and attached it
Thanks,
Abbas- This topic was modified 1 month, 1 week ago by
Abbas.
- This topic was modified 1 month, 1 week ago by
Abbas.
- This topic was modified 1 month ago by
Abbas.
Attachments:
You must be logged in to view attached files.March 4, 2025 at 10:29 am #37689Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
March 4, 2025 at 10:35 am #37690Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
March 4, 2025 at 11:15 pm #37692Abbas
ParticipantFirst of all, I would like to thank you very much for your help, and I apologize for my poor English.
It seems that the idea did not get across correctly.
The code that you put works if the verification is works on one option box that contains several items, but in my form, each option box contains only one item, meaning that each option box has a different ID, which means that there is a need to create an array for verification according to my simple understanding.
- This reply was modified 1 month ago by
Abbas.
March 4, 2025 at 11:24 pm #37693Abbas
ParticipantI also modified the code by changing the IDs, but unfortunately when I press the submit button the condition does not work and the total is not checked if it is equal to 100 or not
This is the code I modified the IDs and added to Code Snippets:
add_filter(‘quform_post_validate_141’, function (array $result, Quform_Form $form) {
$ids = array(‘141_11’, ‘141_19’, ‘141_29’, ‘141_40’, ‘141_50′,’141_60′,’141_70′,’141_150’);
$total = 0;foreach ($ids as $id) {
$element = $form->getElement(“quform_$id”);if ($element instanceof Quform_Element_Field && !$element->isEmpty()) {
$total += (int) $element->getValue();
}
}if ($total > 100) {
$result = array(
‘type’ => ‘error’,
‘error’ => array(
‘enabled’ => true,
‘title’ => ”,
‘content’ => ‘Total goal importance ratio cannot exceed 100’
)
);
}return $result;
}, 10, 2);Any advice?
March 5, 2025 at 1:28 am #37694Abbas
ParticipantUpdate:
Thee code snippet for the second requirement works properly.
I made a small change to the code and it works for me.
I changed:
if ($total > 100)
to
if ($total != 100)And it works properly
Very grateful to you
Hope you help me restrict the number of options selected
March 5, 2025 at 2:57 am #37695Abbas
ParticipantI simulated the code you put in to check the total goals and wrote the following code to check if between 4 and 6 options are selected:
add_filter(‘quform_post_validate_141’, function (array $result, Quform_Form $form) {
$checkboxIds = array(‘141_10’, ‘141_23’, ‘141_34’, ‘141_46’, ‘141_57’, ‘141_68’, ‘141_79’, ‘141_90’, ‘141_101’, ‘141_114’);
$selectedCount = 0;
foreach ($checkboxIds as $id) {
$element = $form->getElement(“quform_$id”);if ($element instanceof Quform_Element_Checkbox && $element->isChecked()) {
$selectedCount++;
}
}if ($selectedCount < 4 || $selectedCount > 6) {
$result = array(
‘type’ => ‘error’,
‘error’ => array(
‘enabled’ => true,
‘title’ => ‘Error’,
‘content’ => ‘You must select between 4 and 6 options.’
)
);
}return $result;
}, 10, 2);but it appears to error:
There was a problem
Ajax ErrorAny advice?
March 5, 2025 at 10:16 am #37698Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
March 5, 2025 at 10:40 am #37701Abbas
ParticipantHi Ally
I modified it as you mentioned, and the code is now working properly
I am grateful to you, and very happy with this support
Thank you very much
Abbas - This topic was modified 1 month, 1 week ago by
- AuthorPosts
- You must be logged in to reply to this topic.