Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!