Apply jquery validation on bootstrap drop-down menu -


i have following bootstrap drop-down menu:

<div class="form-group">     <label for="name" class="col-md-3 control-label">pens</label>     <div class="col-md-9">         <div class="btn-group btn-input">             <button id="penslist"  type="button"                 class="btn btn-default dropdown-toggle form-control"                 data-toggle="dropdown">                 <span data-bind="label">nothing selected</span>                  <span class="caret"></span>             </button>             <ul id="elements" class="dropdown-menu">               <li id="my_li_id_0"><a href="#">nothing selected</a></li>               <li id="my_li_id_1"><a href="#">ballpoint pens</a></li>               <li id="my_li_id_2"><a href="#">rollerball pens</a></li>               <li id="my_li_id_3"><a href="#">fountain pens</a></li>               <li id="my_li_id_4"><a href="#">custom pens</a></li>             </ul>         </div>     </div> </div> 

and script making drop down:

$(document.body).on('click', '.dropdown-menu li', function(event) {      var $target = $(event.currenttarget);       $target.closest('.btn-group').find(         '[data-bind="label"]').text(              $target.text()).end().children('.dropdown-toggle').dropdown('toggle');          return false; }); 

how can apply jquery validation on it, raising error on element id my_li_id_0?

the jquery validate plugin can only validate input, textarea, , select elements contained within form container. cannot validate unordered list. ( edit: in newer versions, can validate kinds of elements contenteditable attribute. )

you'll have construct workaround. this...

  1. bind event bootstrap drop-down menu that's fired whenever user makes or changes selection.

  2. whenever event #1 fired, copy desired value list hidden input element.

  3. set plugin's ignore option [] nothing ignored , hidden input elements validated.

  4. set validation rules on name of hidden input element.

  5. use .valid() method within function on #2 above trigger validation test on hidden input element whenever selection changed.

  6. use conditional within plugin's errorplacement callback function put validation message in location more relevant bootstrap pulldown menu. you'll need conditional in order preserve default error placement code other elements.


btw: numerical id...

<li id="1"> 

...might technically correct in html5, however, id starting number problem in other areas. more info: what valid values id attribute in html?


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -