2016-09-30 2 views
0

enter image description herevalidation JQuery pour sélection au moins une case à cocher

Je veux valider au moins une case à cocher doit être sélectionnée ou montrer autre erreur et mise au point. Voici ma forme:

<div class="form-group">Eligible Branch 
    <select id="eligiblebranch" multiple="multiple" class="form-control" > 
     <?php $branch = $conn->query("SELECT * FROM r_branch ORDER BY id_branch ASC"); 
      while ($branchresult = $branch->fetch_assoc()) { ?> 
      <option value="<?php echo $branchresult['id_branch']; ?>"> <?php echo $branchresult['branch_name']; ?> </option> 
     <?php } ?> 
    </select> 
</div> 

Et voici ce que j'ai essayé:

//Eligible branch 
var eligiblebranch = $("#eligiblebranch").val(); 
if(eligiblebranch=='0') 
{ 
    $("#eligiblebranch").css({"border-style": "solid", "border-color": "red"}); 
    $("#showMessage").html('Please Enter Eligible Branch'); 
    $("#eligiblebranch").focus(); 
    return false; 
    }else{ 
    $("#eligiblebranch").css({"border-style": "solid","border-color": "#E9E9E9"}); 
} 
+0

Je ne trouve aucune case dans votre code? –

+0

multiselect, au moins un select? –

+1

Avez-vous essayé l'entrée $ ("[id * = eligiblebranch]: cochée"). Length'? –

Répondre

2

Voici un code pour vous de commencer ...

function validate(){ 
 
var gR = $("#eligiblebranch :checked"); 
 
    if(gR.length==0){ 
 
    alert("Please select atleast one option"); 
 
    } else { 
 
    alert("Can submit form"); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group">Eligible Branch 
 
    <select id="eligiblebranch" multiple="multiple" class="form-control" > 
 
     <option value="A">A</option> 
 
     <option value="B">B</option> 
 
     <option value="BB">BB</option> 
 
     <option value="AA">AA</option> 
 
    </select> 
 
</div> 
 
<input type="button" onclick="validate()" value="Submit" />