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...
bind event bootstrap drop-down menu that's fired whenever user makes or changes selection.
whenever event #1 fired, copy desired value list hidden
input
element.set plugin's
ignore
option[]
nothing ignored , hiddeninput
elements validated.set validation rules on
name
of hiddeninput
element.use
.valid()
method within function on #2 above trigger validation test on hiddeninput
element whenever selection changed.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
Post a Comment