I have a problem using bootstrap select plugin and jQuery validation. when i select value, error message This field is required
not remove While normal validation(without bootstrap select plugin) after select value error message auto remove. how to fix this problem?
JSFIDDLE
HTML:
<form id="myform"> <----With Select Plugin
<select name="year" class="selectpicker">
<option value="">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
<br/>
<input type="submit" />
</form>
<form id="myform1"> <----Without Select Plugin
<select name="fruit">
<option value="">Year</option>
<option value="1">1955</option>
<option value="2">1956</option>
</select>
<br/>
<input type="submit" />
</form>
JS:
$(document).ready(function () {
$('.selectpicker').selectpicker();
$('#myform').validate({ // initialize the plugin
ignore: [],
rules: {
year: {
required: true
}
},
errorPlacement: function(error, element) {
if (element.attr("name") == "year") {
error.insertAfter(".bootstrap-select");
} else {
error.insertAfter(element);
}
},
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
});
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#myform1" ).validate({
rules: {
fruit: {
required: true
}
}
});
Adding an else statement to return the value of your error message span to a non-error state could correct this. this should display your error msg and return it to blank when a valid input is present.
You can only disable it after the form has successfully passed validation. Use the plugin's submitHandler option for this as it's fired on a button click only when the form has passed validation. $(). ready(function() {... is not recommended as per jQuery documentation.
valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
You need to check the validity on change
$('.selectpicker').selectpicker().change(function(){
$(this).valid()
});
Demo: Fiddle
Try this
$('select').change(function(){
if ($(this).val()!="")
{
$(this).valid();
}
});
Here is the Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With