I have some inputs using the chosen plugin that I want to validate as \"required\" on the client side. Since \"chosen\" hides the actual select e
In the year 2018 I am verifying that jQuery validate is complaining about an input field with no name. This input field is appended by the jQuery Chosen plguin.
This bug is happening before anything else when using chosen.
I spent about a day working on this and had no luck at all! Then I looked at the source code Vtiger was using and found gold! Even though they were using older versions they had the key! you have to use
data-validation-engine="validate[required]"
If you don't and you have it where classes are passed through the class for the select gets applied to the chosen and it thinks that your chosen never gets updated. If you don't pass the class onto the chosen this should be a problem, BUT if you do this is the only way it will work.
This is with chosen 1.4.2 validationEngine 2.6.2 and jquery 2.1.4
// binds form submission and fields to the validation engine
jQuery("#FORMNAME").validationEngine({
prettySelect : true,
useSuffix: "_chosen"
//promptPosition : "bottomLeft"
});
After calling chosen() use below jquery method
$("#id").css("display":"").addClass("sr-only");
sr-only is boot strap class
I ended up solving it by doing the following:
This was for Chosen v1.8.7:
this.form_field_jq.hide().after(this.container),
this.form_field_jq.addClass('chosen-master').after(this.container),
.chosen-master {
display: inline-block !important;
width: 1px;
height: 1px;
margin: 0;
padding: 0;
border: 0;
}
Reference: https://stackoverflow.com/a/64491538/7353382
You can also try this:
$('form').on('submit', function(event) {
event.preventDefault();
if($('form').valid() == true && $('.select-chosen').valid() == true){
console.log("Valid form");
} else {
console.log("Invalid form");
}
});
Remember to add .select-chosen
class on each select
s.
$('.select-chosen').valid()
forces the validation for the hidden select
s
http://jsfiddle.net/mrZF5/39/
You might also running into trouble that error message is displayed before Chosen dropdownlist. I found out the solution to resolve this issue and paste my code here to share with you
HTML:
<label class="col-sm-3">Select sponsor level *</label>
<asp:DropDownList ID="ddlSponsorLevel" runat="server" CssClass="col-sm-4 required" ClientIDmode="Static" />
<label class="error" id="ddlSponsorLevel-error" for="ddlSponsorLevel"></label>
Javascript:
if (!$('#ddlSponsorLevel').valid()) {
$('#ddlSponsorLevel-error').text("You must choose a sponsor level");
return false;
}
Jquery Validation actually added a hidden label html element. We can re-define this element with same ID on different place to overwrite original place.