Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple checkbox validation using javascript

Status
Not open for further replies.

dougancil

IS-IT--Management
Mar 31, 2009
44
US
I am trying to write a page that requires a Pass/Fail for conditions that are done everyday and then posting them to a database. The problem I'm having is the validation of the checkboxes. Find the code below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<style type="text/css">
#form1 {
text-align: left;
width: 550px;
margin-left: 0px;
}
</style>
</head>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('dailycheck');
var checkCount = 0;
for (var i = 0; i < chks.length; i++)
{
if (chks.checked)
{
checkCount++;
}
}
if (checkCount < 8)
{
alert("Please select Pass or Fail for each.");
return false;
}
return true;
}
</script>
<body style="margin-left: 350px; margin-top: 60px">

<form name="form1" onSubmit="return validate()">
<table style="width: 81%; text-align: left">
<tr>
<td style="width: 285px">Called 512-483-9014</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px"/>Fail
</td>
</tr>
<tr>
<td style="width: 285px">Called 512-381-0115</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
<tr>
<td style="width: 285px">Checked VLPortal for Calls since Midnight</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
<tr>
<td style="width: 285px">&nbsp;</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
</table>
<table style="width: 81%; text-align: left">
<tr>
<td style="width: 285px">&nbsp;</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
<tr>
<td style="width: 285px">&nbsp;</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
<tr>
<td style="width: 285px">&nbsp;</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</tr>
<tr>
<td style="width: 285px">&nbsp;</td>
<td>
<input name="dailycheck" type="checkbox" value="pass" style="width: 23px" />Pass
<input name="dailycheck" type="checkbox" value="fail" style="width: 23px" />Fail
</td>
</table>
<p style="width: 52px; margin-left: 280px">
<input name="Submit" type="submit" value="Formsubmit1" style="text-align: center; margin-left: 0px" /></p>
<p>&nbsp;</p>
</form>

</body>

</html>

Multiple checkbox validation is new to me and I'm just needing some help in the correct direction to go. Can anyone assist me please?

Thank you
 
So, if someone checks both Pass and Fail for the same item, they get credit for checking 2 checkboxes? I'd use radio buttons instead, and the validation would verify that one of each set was selected. You'll have to use a different name for each set, of course.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top