javascript - HTML:JS:BOOTSTRAP ; validate the multiselect dropdown - Stack Overflow

In my web application we have used the twitter-bootstrap theme. I have to do client side validation for

In my web application we have used the twitter-bootstrap theme. I have to do client side validation for one form in my application. The form includes text fields,checkboxs and multiselect dropdowns. I can validate textfield and checkboxes but i cant validate the dropdown.

 <select id="users" name="username[]"  class="chzn-select span2" multiple="multiple">  
     <option value="sdfsd">sdfsdfs</option>
           .... 
   </select>

But during runtime the select tag is hide and they generate <li> tags. I cant use document.getElementByID();

Please provide me the best way to do the validation.

In my web application we have used the twitter-bootstrap theme. I have to do client side validation for one form in my application. The form includes text fields,checkboxs and multiselect dropdowns. I can validate textfield and checkboxes but i cant validate the dropdown.

 <select id="users" name="username[]"  class="chzn-select span2" multiple="multiple">  
     <option value="sdfsd">sdfsdfs</option>
           .... 
   </select>

But during runtime the select tag is hide and they generate <li> tags. I cant use document.getElementByID();

Please provide me the best way to do the validation.

Share Improve this question edited Dec 5, 2013 at 16:09 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked Dec 5, 2013 at 10:11 user3069614user3069614 632 silver badges5 bronze badges 1
  • Please do not use jquery-validate tag when the question has noting to do with this plugin. Edited. Thanks. – Sparky Commented Dec 5, 2013 at 16:11
Add a ment  | 

2 Answers 2

Reset to default 4

Try this

HTML

<form action="#">
<select id="users" name="username[]"  class="chzn-select span2" multiple="multiple"> 
      <option value=""></option>  
     <option value="sdfsd">sdfsdfs</option>
           .... 
</select>
<input type="submit" id="add_btn">
</form>

Script

$('#add_btn').on('click', function(e) {
        if($('#users').val() == null && $('#users_chosen').is(':visible')) {
        alert('You must choose division');
        }

});

DEMO

OR

$(function(){
    $('form').submit(function(){
         var options = $('#users > option:selected');
         if(options.length == 0){
             alert('no value selected');
             return false;
         }
    });
});

DEMO

//You can use Js for this
    var ptype=document.getElementById('username').options[document.getElementById('username').selectIndex].value;

if(ptype=""){
document.getElementById('username').style.display='none';
alert('Please select field');
document.getElementById('username').focus();
return false;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744762482a4592253.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信