Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can highlight as required field with "chosen" Plugin?

Following dropdown code and I want to this field as required but there is no checking for validation.

<select id="mood" rel="chosen" required="required" name="moodName">
<option value="">Select Option</option>
<option value="69">AGITATED</option>
<option value="115">ALOOF</option>
<option value="46">AMUSED</option>
</select>

Validation Code

$('#Form').validate();

How to validate as required field of chosen field when Submit form? like "This field is required".

like image 603
Sadikhasan Avatar asked Sep 08 '14 05:09

Sadikhasan


People also ask

How do you use the chosen select?

InitializeCreate your <select> element and call chosen() on the selector. In the example, I have created one single and multiple options <select> elements. In the script, I call chosen() method on the select selector and to set the width of the select element define width option.

What is chosen js?

Chosen is a JavaScript plugin that makes select boxes user-friendly. It is currently available in both jQuery and Prototype flavors.


2 Answers

I ended up using the workaround mentioned in https://github.com/harvesthq/chosen/issues/2075#issuecomment-193805605

That is, find the line in chosen.jquery.js that says

this.form_field_jq.hide().after(this.container);

and replace it by

this.form_field_jq.css('position', 'absolute').css('opacity', 0).after(this.container);

It is a hack to "make invisible" the select element instead of telling the browser to hide it. Hence Chrome will be able to find the element when it needs to show the "please select an item in the list" message.

like image 166
AlejandroVD Avatar answered Sep 27 '22 18:09

AlejandroVD


If you're using Chosen for all select elements, you can use this CSS to change make it visible (to DOM), but no opacity, no height, absolute position.

These CSS selectors target invalid select elements, with one of them targeting multiple adding a 15px margin-top to center it on the multiselect elements.

select:invalid {
    height: 0px !important;
    opacity: 0 !important;
    position: absolute !important;
    display: flex !important;
}

select:invalid[multiple] {
    margin-top: 15px !important;
}

Demo: http://jsfiddle.net/tripflex/2zdeu9oc/

like image 42
sMyles Avatar answered Sep 27 '22 19:09

sMyles