Limit number of choices in select field

Hi!

I am a newbie with Scriptcase (sorry!), but is it possible to limit the number of choices a user can make in a multiselect field? The values are loaded via a sql from a database and can contain like 20 choices, but the uses is allowed only to choose 3 of them. I hope I make myself clear. Thanks!

since you use mysql, just add the LIMIT to your sql string.

SELECT
    select_list
FROM
    table_name
LIMIT [offset,] row_count;

Hi! Thanks for your reply, but what I meant is: show all 20 choices, but allow the user to only select 3. Is that possible?

hi, you can create a global “var_user_limit” for user and in your select Limit [offset, ] [var_user_limit];

Hi, I am so sorry for my lack of communicating clear:

I want to show via select like 10 options to the user: red, yellow, green, purple, black, white, blue, orange, taupe, grey

AND

he is only allowed to select 3 of those colours.

How to do that?

I understand with LIMIT 3 it only shows 3 colours.

I found this and will do the job.

https://stackoverflow.com/questions/30474614/multiple-select-limit-number-of-selection

Any help on how to implement this function into SC?

here is how to do it

For now it only works with a SELECT in a FORM single/mult/grid. Since the initial value of a SELECT in a single form and the multi/grid forms you will have to change the value from 0 + 3 (max values of options to be selected) in a SINGLE form to 25 + 3 in a multi/grid form. Otherwise the code wont be executed. When maximum options are selected all the other options will be deselected. Selecting more than the maximum allowed will deselect the whole list and let the user to choose again. A message is shown aswell.

I wanted to upload some screenshots but since this forum has updated this is not possible for the moment.

In OnscriptInit

?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    $( document ).ready(function() {
        // $("select").on('click', 'option', function() {
        $("select").on('click', 'option', function() {
            // CHECK VALUE OF SELECT WHEN TRIGGERED 
            //    * SINGLE FORM USE 0 (INITIAL VALUE SELECT) + 3 (MAX OPTIONS SELECTED);
            //    * MULTI FORM/EDITABLE GRID/EDITABLE GRID VIEW USE 25 (INITIAL VALUE SELECT) + 3 (MAX OPTIONS SELECTED);

            // alert($("select option:selected").length);
            if (($("select option:selected").length == 0 + 3)){
                // $(this).removeAttr("selected");
                $('select option[value!=\'\']').not(':selected').prop("disabled",true);
            } else if ($("select option:selected").length > 0 + 3 ) {
                // USE of SWEETALERT2 SC 9.4 else add LIB
                Swal.fire({
                    type: 'error',
                    title: 'Oops...',
                    text: 'Only 3 choices possible!',
                    footer: 'list will be deleted'
                })
                $('select option[value!=\'\']').prop("selected",false);
            } else {
                $('select option[value!=\'\']').prop("disabled",false);
            }
        });
    });
</script>
<?php