Results 1 to 2 of 2

Thread: javascript - validating checkboxes in loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    4

    javascript - validating checkboxes in loop

    I am having trouble with my code. I am generating a form from an oracle database. I want to validate my form when I hit submit to check to see if any of the checkboxes have been selected. If none of them have been selected, then a pop up message will occur. Right now, if I have a checkbox selected, my message box pops up (which it shouldn't) and then it goes to the my insert_employee.asp which it isn't supposed to. Any help for pointing me in the right directions would be great.

    VB Code:
    1. <!-- Begin
    2. function validform() {
    3. var count
    4. count = document.AddEmployee.hidcount.value
    5. //alert (count);
    6.             for ( var i = 0; i <= count ; ++i )
    7.   if (document.AddEmployee.addempcheckbox.value == false) {
    8.   alert ("No employees were selected!");
    9.   return false;
    10.   }
    11.  return true;
    12.  }
    13.  function goSearch() {
    14.   if (validform()) {
    15.   document.AddEmployee.action = "Insert_Employee.asp"
    16.   document.AddEmployee.submit()
    17.   }
    18.  return false;
    19.  }
    20. //  End -->
    21. </SCRIPT>
    22. </HEAD>
    23. <BODY">
    24.  
    25. <% If rsEmp.BOF and rsEmp.EOF Then%>
    26. <div class="text">We did not find a match. <a href="searchemployee.asp">Search Again.</a></div>
    27. <%Else%>
    28. <%If Not rsEmp.BOF Then%>
    29.  
    30. <FORM NAME="AddEmployee" ACTION="" METHOD="post">
    31. <table border="1" cellspacing="0" cellpadding="3" valign="top" align="center" width="600">
    32.  <tr bgcolor="#C9DBF7">
    33.   <td valign="bottom" align="center" width="150">
    34.       <div class="smallboldtext">Add to Phone List</div>
    35.     </td>
    36.     <td valign="bottom" align="center" width="150">
    37.       <div class="smallboldtext">Employee Name</div>
    38.     </td>
    39.   </tr>
    40. <%
    41. count=-1
    42. Do While Not rsEmp.EOF %>
    43.   <tr bgcolor="#ffffff">
    44.     <td valign="bottom" align="center" width="150"><input type="checkbox" name="addempcheckbox">
    45.  
    46.     </td>
    47.     <td valign="bottom" align="center" width="150">Employee Name pulled from database is here
    48.  
    49.     </td>
    50.   </tr>
    51. <% rsEmp.MoveNext %>
    52. <%
    53. count=count+1
    54. Loop %>
    55. <input type=hidden value="<%=count%>" name="hidcount">
    56. </table>
    57.  
    58. <center><input type="image" src="images/submit.gif" onfiltered="goSearch()">;</center>
    59.  
    60. </FORM>
    61. <%End If%>
    62. <%End If%>
    63. <%
    64. rsEmp.Close
    65. cn.Close
    66. %>

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    for ( var i = 0; i <= count ; ++i ) 
      if (document.AddEmployee.addempcheckbox.value == false) {
      alert ("No employees were selected!");
      return false;
      }
    You're only evaluating a single control there!
    Code:
    <HTML>
    
    <HEAD>
    <SCRIPT Language="Javascript">
    <!--
    
    	function EvalBoxes() {
    		var intCounter = 0;
    		var blnAllUnchecked = true;
    		var ctlElementName = '';
    
    		for (intCounter=0; intCounter<=4; intCounter++) {
    			ctlElementName = 'chkTemp' + (intCounter+1);
    
    			if (document.getElementById(ctlElementName).checked == true) {
    				blnAllUnchecked = false;
     				break;
    			}
    		}
    		
    		alert('All boxes unchecked = ' + blnAllUnchecked);
    	}
    
    //-->
    </SCRIPT>
    </HEAD>
    
    <BODY>
    	<INPUT Type="Checkbox" id="chkTemp1">Test 1<BR>
    	<INPUT Type="Checkbox" id="chkTemp2">Test 2<BR>
    	<INPUT Type="Checkbox" id="chkTemp3">Test 3<BR>
    	<INPUT Type="Checkbox" id="chkTemp4">Test 4<BR>
    	<INPUT Type="Checkbox" id="chkTemp5">Test 5<BR>
    
    	<BR><BR>
    
    	<INPUT Type="Button" id="cmdEvalBoxes" 
    	Value="Click Me!" onClick="Javascript: EvalBoxes();">
    </BODY>
    </HTML>

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width