multiple select (enforce at least one selection)

admin

Administrator
Staff member
Is it possible to declare a SELECT box to enforce at least one selection when using the MULTIPLE keyword<br />
<br />
At the moment, i have a select box which is declare as follows:<br />
<br />
<SELECT name"whatever" size="5" MULTIPLE><br />
<br />
...<br />
<br />
</SELECT><br />
<br />
<br />
which is working perfectly except the user can un-select so that no selection has been made.<br />
<br />
apart from validating the form using javascript, how can I enforce the SELECT box to have at least one selection?<!--content--><select name="color"><br />
<option value="Red" selected>Red</option><br />
<option value="Blue">Blue</option><br />
<option value="Yellow">Yellow</option><br />
</select><br />
<br />
<br />
This would automatically select Red when the page loads, and there'd be no way the user could send in the form with the field having nothing selected.<!--content-->this does not solve my problem, as I need the user to select MULTIPLE options, hence the MULTIPLE keyword which I have used in my SELECT declaration. <br />
<br />
I realise that in your solution, it would be impossible forthe user to submit the form whilst having no selection for the SELECT box, however, if you include the MULTIPLE keyword, you will find, that not only can the user select multiple options (as per my requirement), but the user can select 0 options - by unselecting the default selected option.<br />
<br />
THis is my problem.. I require the user to select [1 .. m] options..<!--content-->so in addition to my previous post:<br />
<br />
<select MULTIPLE name="color"><br />
<option value="Red" selected>Red</option><br />
<option value="Blue">Blue</option><br />
<option value="Yellow">Yellow</option><br />
</select><br />
<br />
The above code is the same as what I have now.. (the difference to your version is the MULTIPLE keyword)<br />
<br />
This enables the user to make multiple selections, however, this enables the user to unselect the default option, and therefore, selection NOTHING.<br />
<br />
I need to enforce at least one selection, how do I do it?<!--content-->Oops, sorry I missed the multiple part in the original post. I don't think there is a way to do this without using JavaScript unfortunately.<!--content-->I noticed once that instead of using the keyword MULTIPLE like that, it was done like this<br />
<br />
multiple = "multiple"<br />
<br />
and it had the same effect as just using MULTIPLE<br />
<br />
Perhaps you can include parameters for the MULTIPLE keyword doing it the 2nd way?<br />
<br />
anyone?<!--content--><select multiple="multiple" name="color"> <br />
<option value="Red" selected>Red</option> <br />
<option value="Blue">Blue</option> <br />
<option value="Yellow">Yellow</option> <br />
</select> <br />
<br />
which has the same effect as:<br />
<br />
<select MULTIPLE name="color"> <br />
<option value="Red" selected>Red</option> <br />
<option value="Blue">Blue</option> <br />
<option value="Yellow">Yellow</option> <br />
</select> <br />
<br />
maybe you can add something in the ="multiple" part.. like number meaning min selection count?<br />
<br />
help?!<!--content-->Check out this link (<!-- m --><a class="postlink" href="http://www.w3.org/TR/xhtml1/#h-4.5">http://www.w3.org/TR/xhtml1/#h-4.5</a><!-- m -->). Should explain the multiple="multiple" thing.<!--content-->
 
Back
Top