HTML Multiselect Limit
You can use jQuery
$("select").change(function () {
if($("select option:selected").length > 3) {
//your code here
}
});
Multiple Select limit number of selection
Try this...
Check length and deselect after reach maximum select
<select id="userRequest_activity" required name="activity[]" class="multiselect form-control" multiple="multiple">
<option value="2">Bungee Jumping</option>
<option value="3">Camping</option>
<option value="5">Mountain Biking</option>
<option value="6">Rappelling</option>
<option value="7">Rock Climbing / Bouldering</option>
<option value="8">Skiing</option>
<option value="10">Wild Life (Safari)</option>
<option value="11">Canoeing & Kayaking</option>
<option value="12">Rafting</option>
<option value="13">Sailing</option>
<option value="14">Scuba Diving</option>
<option value="15">Snorkeling</option>
<option value="16">Surfing</option>
<option value="18">Hang Gliding</option>
<option value="19">Hot-air Ballooning</option>
<option value="20">Micro-light Aircrafts</option>
<option value="21">Paragliding</option>
<option value="22">Paramotoring</option>
<option value="23">Parasailing</option>
<option value="24">Skydiving / Parachuting</option>
<option value="25">Zip-line / Flying Fox</option>
<option value="26">Caving</option>
<option value="27">Cycling</option>
<option value="28">Fishing & Angling</option>
<option value="29">Motorbike trips</option>
<option value="30">Nature Walks</option>
<option value="31">Road Trips</option>
<option value="32">Zorbing</option>
<option value="33">Trekking Hiking and Mountaineering</option>
<option value="34">Backpacking</option>
<option value="61">Water</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
var last_valid_selection = null;
$('#userRequest_activity').change(function(event) {
if ($(this).val().length > 3) {
$(this).val(last_valid_selection);
} else {
last_valid_selection = $(this).val();
}
});
});
</script>
DEMO:http://jsfiddle.net/9c3sevuv/
how to select limit number of options in multiple select plugin?
EDIT :
There working jsFiddle example with multi-select plugin
var limit = 3;
var $multiSel = $("select").multipleSelect({
placeholder: "Here is the placeholder",
width: 200,
filter: true,
selectAll: false,
onClick: function(view) {
var $checkboxes = $multiSel.next().find("input[type='checkbox']").not(":checked");
var selectedLen = $multiSel.multipleSelect('getSelects').length;
if (selectedLen >= limit) {
$checkboxes.prop("disabled", true);
} else {
$checkboxes.prop("disabled", false);
}
}
});
Bootstrap Multiselect Limit Issue
You Need to Block Shift key When Clicking the list option.
Try the Below jQuery.
Please Check it Out Here -> JSFIDDLE
var shiftClick = jQuery.Event("click");
shiftClick.shiftKey = true;
$(".multiselect-container li *").click(function(event) {
if (event.shiftKey) {
//alert("Shift key is pressed");
event.preventDefault();
return false;
}
else {
//alert('No shift hey');
}
});
Dropdown with multiple selection limit
Well according to https://react.semantic-ui.com/modules/dropdown#dropdown-example-multiple-selection you need to create controlled component, which means you will bind value={this.state.selectedItems}
then you will bind onChange={(e,data) => { this.handleChange(e,data )}
and in your code
onChange (e, data) {
const currentItems = this.state.selectedItems
if (currentItems.length <= MAX_SELECTION ) {
currentItems.push(data)
this.setState({
selectedItems: currentItems
})
}
}
this will crate controlled component which will allows you to control state by yourself, and you will limit changing state, probably you will need to also handle removing items from state inside this onChange event.
Limit selected option in bsmultiselect
There are possible two behaivours:
- allow 6th selected but show it as invalid
Here are two different situations: is bsmultiselect is over the select
(and results goes to form) or over the JS object.
In first case use "custom Html ValidationAPI" multiSelect.validationApi.setCustomValidity(...)
;
In second case use "custom Bootstrap validation" (pass in the configuration {getValidity: fuction( return true; //or false ) }
)
- do not allow 6th selected at all
I this case pass the setSelected over the configuration and return false if you want to cancel the 6th element selection
{
setSelected(){
if (getSelectedCount(options) ==5) // write it getSelectedCount, just loop through your data or query bsMultiSelect.picksCount;
return false;
}
}
Related Topics
CSS to Align Label and Inputs on Form
<Form Method="Link" > or <A>? What's The Difference
Why Is Box-Sizing Acting Different on Table Vs Div
Preserve Line Breaks in Textarea
Items That Span All Columns/Rows Using CSS Grid Layout
What Is The Max Number of Files to Select in an HTML5 [Multiple] File Input
Get Parameters in The Url with Codeigniter
Button Image as Form Input Submit Button
CSS - Inline Elements Ignoring Line-Height
<Table> with Fixed <Thead> and Scrollable <Tbody>
<Div> into a <Tr>: Is It Correct
How to Always Show Up/Down Arrows for Input "Number"
How to Force Download with HTML/JavaScript
HTML5 I Tag Validity with Icons
How to Remove The Dotted Line Around The Clicked a Element in HTML