I'm working with Bootstrap, selectpicker, and jQuery Validation plugins.
The Select List:
<div class="form-group" id="content">
<select id="country" name="country" required>
<option value=''>Select</option>
<option value='1'>USA</option>
</select>
</div>
The Selectpicker:
$('select').selectpicker();
To jQuery Validation need this to can see the Select List:
$('#frm-name').validate().settings.ignore = ':not(select:hidden, input:visible, textarea:visible)';
And then Validation:
$("#frm-name").validate({
rules: {
"country": {
required: true
}
},
errorPlacement: function(error, element) {
error.insertAfter("#content");
}
})
The error never shows.
What did I miss?
I'm not sure where you're going wrong with all this because it's working for me.
Some tips:
1) You don't need to use
.validate().settings.ignorewhen you can just use theignoreoption within your.validate()call.2) Although no harm is done, your field
nameofcountrydoes not need to be enclosed in quotes within.validate().3) You do not need the
requiredattribute inside your<select>element when you've already declared therequiredrule within.validate().4) When using the
errorPlacementcallback function, take advantage of theerrorandelementarguments since the same function is used generically for all of your form elements. By specifying theidof a specific element, you'll be forcing all error messages for all input elements into the same exact location.5) Since your
selectelement is hidden by theselectpickerelement and theselectpickerelement comes after the hiddenselect, you'll need to adjust your jQuery to find the proper location in the DOM for your error placement.Since your
selectpickeris the next element,element.next().after(error)will insert the error message after the next element in the DOM tree. Useif ($(element).is('select'))to test if the input is aselectelement so as not to interfere with the error placement of the other input elements.Full code:
Working DEMO: http://jsfiddle.net/Bccq7/