Minimum and maximum selected checkboxes

Home Forums Quform WordPress Minimum and maximum selected checkboxes

This topic is: resolved
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #37682
    Abbas
    Participant

    Hi,

    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.
    #37689
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37690
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37692
    Abbas
    Participant

    First 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.
    #37693
    Abbas
    Participant

    I 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?

    #37694
    Abbas
    Participant

    Update:

    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

    #37695
    Abbas
    Participant

    I 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 Error

    Any advice?

    #37698
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #37701
    Abbas
    Participant

    Hi 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

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy