I am using select2 multi select in my MVC application. Its working absolutely fine. But the usual behavior of required attribute is not working for select2 inputs.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Thanks Yuki for your reply. Actually i want to make border of select box red. As jquery required is working for other html elements. I want to have same behavior for select box as well.
Select2 is just a normal select control with
multiple property. You can bind it with Model just like a normal select control and do the validations using
Data Annotations.
A similar question is answers in the forum which might solve your problem to -
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Thanks Yuki for your reply. Actually i want to make border of select box red. As jquery required is working for other html elements. I want to have same behavior for select box as well.
If you would like to make border of select box red,you could refer to the my demo I provided before.
You could add the code like below:
var validobj = $("#frm").validate({
onkeyup: false,
errorClass: "myErrorClass",
//When there is an error normally you just add the class to the element.
// But in the case of select2s you must add it to a UL to make it visible.
// The select element, which would otherwise get the class, is hidden from
// view.
highlight: function (element, errorClass, validClass) {
var elem = $(element);
if (elem.hasClass("select2-offscreen")) {
$("#s2id_" + elem.attr("id") + " ul").addClass(errorClass);
} else {
elem.addClass(errorClass);
}
}
});
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
1 Points
2 Posts
Not able to apply css | Required Field validation for Select2 multi select
Mar 01, 2019 07:41 AM|varun3101|LINK
hi all
I am using select2 multi select in my MVC application. Its working absolutely fine. But the usual behavior of required attribute is not working for select2 inputs.
<select id="FamiliesSelect" class="col-xl-10 js-example-basic-multiple" asp-items="@(new SelectList(some items))" required multiple>
</select>
when the FamiliesSelect input is empty, it is not allowing form submit but does not applying css 'input-validation-error'.
Please help. Thanks
Contributor
3710 Points
1431 Posts
Re: Not able to apply css | Required Field validation for Select2 multi select
Mar 01, 2019 09:59 AM|Yuki Tao|LINK
Hi varun3101,
I make a simple demo,you could refer to:
How my demo works:
More details,you could refer to this link:
http://jsfiddle.net/chadkuehn/D7K6U/
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
1 Points
2 Posts
Re: Not able to apply css | Required Field validation for Select2 multi select
Mar 01, 2019 12:18 PM|varun3101|LINK
Thanks Yuki for your reply. Actually i want to make border of select box red. As jquery required is working for other html elements. I want to have same behavior for select box as well.
Participant
1253 Points
926 Posts
Re: Not able to apply css | Required Field validation for Select2 multi select
Mar 01, 2019 03:45 PM|yogyogi|LINK
Select2 is just a normal select control with multiple property. You can bind it with Model just like a normal select control and do the validations using Data Annotations.
A similar question is answers in the forum which might solve your problem to -
Model Binding and SelectList
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Contributor
3710 Points
1431 Posts
Re: Not able to apply css | Required Field validation for Select2 multi select
Mar 04, 2019 02:12 AM|Yuki Tao|LINK
Hi varun3101,
If you would like to make border of select box red,you could refer to the my demo I provided before.
You could add the code like below:
css:
How it works:
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.