Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Multiple <option> required on <select>

Select tag presents has got a long list of options. How can I make sure that at least 2 options are selected by the user? The first option is required, I also need the second to be required.

Here's the code:

    <select name="presents[]" id="presents" class="vv-select2-multiple" multiple required>
        <?php foreach(get_from_group('person', 'any', -1, ['tipo' => 'member']) as $member):?>
            <option value="<?= $member->ID ?>" <?php selected(get_user_person()->ID, $member->ID)?> required><?= $member->post_title; ?></option>
            <?php endforeach;?>
    </select>
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your select tag presents is an array already. Check the array length to make sure you have at least 2 options selected. When the form is submitted, try this on your php page:

$presents = $_POST['presents'];
if (count($presents) >= 2) {
    // Here you are sure that you have at least two options selected.
    // Do whatever you wish here. 
}

Or else, you can also get the same result with jquery.

function checkCount() {
    if($('#presents option').is(':selected')) {
        count = $('#presents option:selected').length;
        console.log(count);
        if (count >= 2) {
            console.log('Selected option are greater than or equal to 2');
        } else {
            console.log('Selected options are less than two');
        }
    } else {
        console.log('No options are selected!!!')
    }
}

$(document).ready(function () {
    checkCount();
    $('#presents').on('change', checkCount);
})

Try this online.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda