By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

(Multi) select list box - selected parameters check

sunsolaris2000
P: 20
I have 2 list boxes in SCAssignment.jsp. One is multi select, one single select.

I use it because user will input how 'students' are assigned to 'courses'.

I want to alert the user to select '1 student to 1/many courses' per form entry. These relations I will enter to the bridge table, "students_courses".

I've seen many examples on forums and Google but none didn't work for me :( I think the problem is when I call the JavaScript function.

This is one variant I used, adapted to my sch. project:

[.....................]

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.    <head>
  3.       <script language="javascript">
  4.  
  5.             function validateListBox(){
  6.                 var lb = document.getElementById('mySelect');
  7.                 var count1 = 0;
  8.                 arrTexts = new Array();
  9.  
  10.                 for(var i=0; i<lb.length; i++)  {
  11.                     if(lb[i] != 0){
  12.                       count1++;
  13.                     }
  14.  
  15.                 }
  16.  
  17.                 if(count1 != 0){
  18.                     alert('empty');
  19.                     return true;
  20.                 }
  21.  
  22.                 return false;        
  23.             }
  24.  
  25.           </script>
  26.           <noscript>Your browser does not support JavaScript!</noscript>
  27.      </head>
  28. [.......................................]
  29.  
  30.        <form action = "AssignmentsList.jsp" method="post" name="entry">
  31.  
  32.                 [.....................]
  33.               <select id = "mySelect" multiple name = "mySelect" size = "5">
  34.                        <% for (Student st : students){%>
  35.                              <option value="<%= st.getStudent_id() %>"><%= st.getStudent_id() %></option>
  36.                                 <%}%>
  37.                          </select>                
  38.  
  39.                  [...................]
  40.                            <input type="button" value = "Link" onClick = "validateListBox()"/>
  41.                            <br>
  42.                            <input type = "reset" value = "Reset"/>
  43.                       [...................]
  44.                           <select  id = "mySelect2" name = "mySelect2" size = "5">
  45.                               <% for (Course cs : courses){%>
  46.                                     <option value="<%= cs.getCourse_id() %>"><%= cs.getCourse_id() %></option>
  47.                                <%}%>
  48.                           </select>
  49.                   [...................]    
  50.  
  51.              </form>
  52.              [.............]            
  53.  
  54.           </body>
  55. </html>
They are displayed in a table so I cut the code a bit with [.....].

I tried with onClick, onSubmit, input type="button", input type="submit", what I've seen by now.
Attached Images
File Type: jpg Untitled.jpg (38.0 KB, 300 views)
File Type: jpg Untitled2.jpg (37.4 KB, 221 views)
Jul 16 '12 #1
Share this Question
Share on Google+
2 Replies


sunsolaris2000
P: 20
I finally find out is:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3.             function validate(){
  4.                 var k=0;
  5.                 if (entry.elements["sid"].selectedIndex == -1) {
  6.                       k++;
  7.                       alert('You cannot select one side only');
  8.                       return false;
  9.                 }
  10.  
  11.                 if (entry.elements["cid"].selectedIndex == -1) {
  12.                       k++;
  13.                       alert('You cannot select one side only');
  14.                       return false;
  15.                 }
  16.  
  17.                 if(k == 0){
  18.                   return true;
  19.                 }
  20.  
  21.                 return false;
  22.             }
  23.  
  24.           </script>
Jul 22 '12 #2

sunsolaris2000
P: 20
I found it out finally:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3.             function validate(){
  4.                 var k=0;
  5.                 if (entry.elements["sname"].selectedIndex == -1) {
  6.                       k++;
  7.                       alert('You cannot select one side only');
  8.                       return false;
  9.                 }
  10.  
  11.                 if (entry.elements["ctitle"].selectedIndex == -1) {
  12.                       k++;
  13.                       alert('You cannot select one side only');
  14.                       return false;
  15.                 }
  16.  
  17.                 if(k == 0){
  18.                   return true;
  19.                 }
  20.  
  21.                 return false;
  22.             }
  23.  
  24.           </script>
Jul 27 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.