(Multi) select list box - selected parameters check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunsolaris2000
    New Member
    • Jan 2012
    • 20

    #1

    (Multi) select list box - selected parameters check

    I have 2 list boxes in SCAssignment.js p. 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_cours es".

    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:

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

    Code:
    <html>
       <head>
          <script language="javascript">
    
    			function validateListBox(){
    				var lb = document.getElementById('mySelect');
    				var count1 = 0;
    				arrTexts = new Array();
    
    				for(var i=0; i<lb.length; i++)  {
    					if(lb[i] != 0){
    					  count1++;
    					}
    					
    				}
    				
    				if(count1 != 0){
    					alert('empty');
    					return true;
    				}
    				
    				return false;        
    			}
    			
              </script>
              <noscript>Your browser does not support JavaScript!</noscript>
         </head>
    [.......................................]
    
           <form action = "AssignmentsList.jsp" method="post" name="entry">
              
                    [.....................]
                  <select id = "mySelect" multiple name = "mySelect" size = "5">
                           <% for (Student st : students){%>
                                 <option value="<%= st.getStudent_id() %>"><%= st.getStudent_id() %></option>
                                    <%}%>
                             </select>                
                                        
                     [...................]
                               <input type="button" value = "Link" onClick = "validateListBox()"/>
                               <br>
                               <input type = "reset" value = "Reset"/>
                          [...................]
                              <select  id = "mySelect2" name = "mySelect2" size = "5">
                                  <% for (Course cs : courses){%>
                                        <option value="<%= cs.getCourse_id() %>"><%= cs.getCourse_id() %></option>
                                   <%}%>
                              </select>
                      [...................]    
                          
                 </form>
                 [.............]            
            
    		  </body>
    </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 Files
  • sunsolaris2000
    New Member
    • Jan 2012
    • 20

    #2
    I finally find out is:

    Code:
    <script language="javascript">
    
    			function validate(){
    				var k=0;
    				if (entry.elements["sid"].selectedIndex == -1) {
    					  k++;
    					  alert('You cannot select one side only');
    					  return false;
    				}
    				
    				if (entry.elements["cid"].selectedIndex == -1) {
    					  k++;
    					  alert('You cannot select one side only');
    					  return false;
    				}
    				
    				if(k == 0){
    				  return true;
    				}
    				
    				return false;
    			}
    			
              </script>

    Comment

    • sunsolaris2000
      New Member
      • Jan 2012
      • 20

      #3
      I found it out finally:

      Code:
      <script language="javascript">
      
      			function validate(){
      				var k=0;
      				if (entry.elements["sname"].selectedIndex == -1) {
      					  k++;
      					  alert('You cannot select one side only');
      					  return false;
      				}
      				
      				if (entry.elements["ctitle"].selectedIndex == -1) {
      					  k++;
      					  alert('You cannot select one side only');
      					  return false;
      				}
      				
      				if(k == 0){
      				  return true;
      				}
      				
      				return false;
      			}
      			
                </script>

      Comment

      Working...