Solved

Restrict multiple category selection

Posted on 2009-03-30
3
288 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issues with Insert statement 12 29
Diminish Pop-up  in 3 seconds 7 47
Html value of radio 14 27
troubleshoot a python script 8 20
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

856 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