I have a problem with select2 and Jquery and Checkboxes. I made a form with some different fields. The validate warning is showed above Select field when I added the validate of checkboxes. When I did not add this code:
[checkboxes validate code][1]
[![enter image description here][1]][1]
the warning is shown under select2 field but the checkboxes warnings are showed wrong: [Wrong warning on checkoboxes][3] I do not why, can somebody look to the code and correct me?
[![enter image description here][2]][2]
[form printscreen][2]
[![enter image description here][3]][3]
You can try this:
<form id="myForm" method="post" name="myForm" action="">
<div class="checkbox-container">
<label for="terms">terms : </label>
<input type="checkbox" name="terms" id="terms">
<div id="errorToShow"></div>
</div>
<button type="submit">save</button>
</form>
Script:
$("#myForm").validate({
rules: {
terms: "required"
},
messages: {
terms: "select the checkbox"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "terms") {
error.appendTo("#errorToShow");
} else {
error.insertAfter(element);
}
}
});
Place the contains id="errorToShow" where you want to show error. This works for me. The error message will be shown in
<div id="errorToShow"></div>
Thank you for your answer. So I do it a little different. The validation code is now:
$("#signupForm").validate({
debug: false,
errorClass: "has-error",
errorElement: "span",
rules: {
miejscowosc: {
required: true,
lettersonly: true,
},
wojewodztwo: {
required: true,
},
errorContainer: $('#errorContainer'),
// enabling this will display error labels in the container error
// errorLabelContainer: $('#errorContainer ul'),
// wrapper: 'li',
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass); //.removeClass(errorClass);
$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
},
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass); //.addClass(validClass);
$(element).closest('.form-group').removeClass('has-error').addClass('has-success');
},
errorPlacement: function(error, element) {
if (element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else if (element.hasClass('select2')) {
error.insertAfter(element.next('span'));
}
else if (element.attr("name") == "zgoda") {
error.insertAfter("#checkboxError");
}
else if (element.attr("name") == "zgoda1") {
error.insertAfter("#checkboxError1");
}
else if (element.attr("name") == "zgoda2") {
error.insertAfter("#checkboxError2");
}
else {
error.insertAfter(element);
}
},
});
and now the warning message is Under select2 so It is Ok.
The code Of select2 is
var Formularz = function () {
return {
//main function to initiate the module
init: function () {
function format(state) {
if (!state.id) return state.text; // optgroup
return state.id.toLowerCase() + state.text;
}
$("#Select_Wojewodztwo").select2({
placeholder: "Wybierz Wojewodztwo",
allowClear: true,
formatResult: format,
width: '100%',
formatSelection: format,
escapeMarkup: function (m) {
return m;
}
});
}
};
}();
jQuery(document).ready(function() {
Formularz.init();
});
the second my problem is has-error does not wotk in select2 field and doesnt highlight it on red.
the code in form is:
<div class="col-sm-12 col-md-4 col-lg-4 form-group">
<div class="col-lg-12 form-label">
Województwo
</div>
<div class="col-lg-12 ">
<select name="wojewodztwo" id="Select_Wojewodztwo" class="select2 form-control selection">
<option value=""></option>
<option value="Dolnoslaskie">Dolnośląskie</option>
<option value="Kujawsko-pomorskie">Kujawsko-pomorskie</option>
<option value="Lubelskie">Lubelskie</option>
<option value="Lubuskie">Lubuskie</option>
<option value="Lodzkie">Łódzkie</option>
<option value="Malopolskie">Małopolskie</option>
<option value="Mazowieckie">Mazowieckie</option>
<option value="Opolskie">Opolskie</option>
<option value="Podkarpackie">Podkarpackie</option>
<option value="Podlaskie">Podlaskie</option>
<option value="Pomorskie">Pomorskie</option>
<option value="Slaskie">Śląskie</option>
<option value="Swietokrzyskie">Świętokrzyskie</option>
<option value="Warminsko-mazurskie">Warmińsko-mazurskie</option>
<option value="Wielkopolskie">Wielkopolskie</option>
<option value="Zachodniopomorskie">Zachodniopomorskie</option>
</select>
</div>
</div>
what do you think about it. What can be wrong here?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.