...

View Full Version : Checkbox validation



osemollie
02-16-2006, 03:42 PM
Dear colleagues,

Am working on a beginners project and I have problems with my checkboxes. These checkboxes are an array and I need to validate them.
- Students are supposed to choose 2 courses per week.
- However Financial Management is taught for a whole week hence if a student chooses this course in week 1, they are NOT allowed to choose another course in that particular week.
- If a student chooses Financial Management in week 1, they MUST choose it again in week 2.

How do I validate these?

_________________________
The code for the table looks like:

____________________
<html>
<head>
</head>

<body>

<form method="POST" action="insert.php">
<table border="0" width="500" id="table1" cellspacing="0" cellpadding="0">
<tr>
<td width="397"><b>Course Name</b></td>
<td width="103"><strong>
Select</strong></td>
</tr>
<tr>
<td width="500" bgcolor="#C0C0C0" colspan="2"><b>WEEK1</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Financial Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Product Marketing
</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Product Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
&nbsp;Loan
Portfolio Audit Toolkit</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Loan Portfolio Audit Toolkit"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
&nbsp;Process Mapping </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Process Mapping"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#C0C0C0"><b>WEEK 2</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management (continued.) </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Financial Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Strategic Marketing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Strategic Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Risk Management </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Risk Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Pilot Testing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Pilot Testing "></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
________________________________

Thanks.

arnyinc
02-16-2006, 04:01 PM
Is there some reason all of your checkboxes have to be named the same thing (i.e. chkCourse[])?

degsy
02-16-2006, 04:57 PM
Try something like this



<html>
<head>
<script type="text/javascript">
function chkFrm(el){
if(el.name == "course"){
count = 0;
str = '';
for(x=0; x<el.elements["chkCourse[]"].length; x++){
if(el.elements["chkCourse[]"][x].checked==true){
str += el.elements["chkCourse[]"][x].value + ',';
count++;
}
}

if(count < 2){
alert("You must choose at least 2");
return false;
}
else if(count > 2){
alert("You can choose a maximum of 2");
return false;
}
else {
alert("You chose " + count + ": " + str.substring(0,str.length-1));
return true;
}
}

if(el.checked){
for(x=0; x<el.form.elements["chkCourse[]"].length; x++){
if(!el.form.elements["chkCourse[]"][x].id.indexOf('fm_wk')){
el.form.elements["chkCourse[]"][x].checked = true;
}
}
}
else{
for(x=0; x<el.form.elements["chkCourse[]"].length; x++){
if(!el.form.elements["chkCourse[]"][x].id.indexOf('fm_wk')){
el.form.elements["chkCourse[]"][x].checked = false;
}
}
}
}
</script>
</head>

<body>
<?php
if(isset($_POST['B1'])){
$count = 0;
$fm_count = 0;
$err = '';
if(is_array($_POST['chkCourse'])){
foreach($_POST['chkCourse'] as $key => $val){
if($val == "Financial Management"){
$fm_count++;
}
$count++;
}

if($fm_count == 1){
$err .= "You must choose Financial Management for both weeks<br>";
}
if($count > 2){
$err .= "You have chosen too many courses";
}
elseif($count < 2){
$err .= "You must choose atleast 2 courses<br>";
}

if(strlen($err) > 0){
echo $err;
}
else{
echo "Form OK!";
}
}
else{
echo "You must choose atleast 2 courses";
}
}
?>
<form method="POST" action="<?=$_SERVER['SCRIPT_NAME']?>" name="course" onsubmit="return chkFrm(this)">
<table border="0" width="500" id="table1" cellspacing="0" cellpadding="0">
<tr>
<td width="397"><b>Course Name</b></td>
<td width="103"><strong>
Select</strong></td>
</tr>
<tr>
<td width="500" bgcolor="#C0C0C0" colspan="2"><b>WEEK1</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" id="fm_wk1" value="Financial Management" onclick="chkFrm(this)"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Product Marketing
</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Product Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
&nbsp;Loan
Portfolio Audit Toolkit</span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Loan Portfolio Audit Toolkit"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
&nbsp;Process Mapping </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value=" Process Mapping"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#C0C0C0"><b>WEEK 2</b></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Financial Management (continued.) </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" id="fm_wk2" value="Financial Management" onclick="chkFrm(this)"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Strategic Marketing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Strategic Marketing"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Risk Management </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Risk Management"></td>
</tr>
<tr>
<td width="397"><span style="font-size: 11px">
Pilot Testing </span></td>
<td width="103">
<input type="checkbox" name="chkCourse[]" value="Pilot Testing "></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum