Solved

Restrict multiple category selection

Posted on 2009-03-30
3
287 Views
Last Modified: 2012-06-27
I have a multiple select option in a form, so the user can choose more than 1 category by holding down control and selecting. This means they can select any number of categories.

Is there any way to restrict this so they can only select say 1 option? Is this possible?
0
Comment
Question by:dartagnon
3 Comments
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24026432
Hi dartagnon,
I'm curious why you not use normal option selection box for this selection instead of listbox? But, here is one of the attempt that restricted only one selection in listbox as requested.
<select id="sel1" multiple="true" onclick="changeOpt(this);">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
 
<script>
function changeOpt(sel){
  var opts='';
  var intSel=0;
 
  for(i=0;i<sel.length;i++){
    if(sel.options[i].selected){
      intSel+=1;
     
     if(intSel>1){
      sel.options[i].selected=false;
     }
    }
  }
}
 
</script>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24026660
If you want to SHOW the options but still only have one selection, just change the size

<select size="4">
<option value="">Please select</option>
 <option value="1">One</option>
 <option value="2">Two</option>
 <option value="3">Three</option>
</select>
 
0
 

Author Comment

by:dartagnon
ID: 24091921
Thanks x com, that worked brilliantly. Here is what I ended up with:
<select name="categoryID" size="6" multiple="true" onclick="changeOpt(this);">
                            <option value="0" selected="selected">-- Select Category --</option>
                            <%
While (NOT categories.EOF)
%>
                            <option value="<%=(categories.Fields.Item("categoryID").Value)%>" ><%=(categories.Fields.Item("categoryname").Value)%></option>
                            <%
  categories.MoveNext()
Wend
If (categories.CursorType > 0) Then
  categories.MoveFirst
Else
  categories.Requery
End If
%>
                          </select>
                                      <script>
function changeOpt(sel){
  var opts='';
  var intSel=0;
 
  for(i=0;i<sel.length;i++){
    if(sel.options[i].selected){
      intSel+=1;
     
     if(intSel>3){
      sel.options[i].selected=false;
     }
    }
  }
}
 
</script>
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question