Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove jquery validation error message after select value

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
}
}
});
like image 779
Pink Code Avatar asked Jan 17 '14 11:01

Pink Code


People also ask

How remove validation message after field is valid?

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.

How can I disable enable submit button after jQuery validation?

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.

What is valid () in jQuery?

valid()Returns: Boolean Description: Checks whether the selected form is valid or whether all selected elements are valid.

Does jQuery validate require a form?

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.


2 Answers

You need to check the validity on change

$('.selectpicker').selectpicker().change(function(){
    $(this).valid()
});

Demo: Fiddle

like image 166
Arun P Johny Avatar answered Oct 12 '22 23:10

Arun P Johny


Try this

$('select').change(function(){
    if ($(this).val()!="")
    {
        $(this).valid();
    }
});

Here is the Fiddle

like image 21
Linga Avatar answered Oct 12 '22 23:10

Linga