Solved

Restrict multiple category selection

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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