Solved

Restrict multiple category selection

Posted on 2009-03-30
3
285 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now