chelvis
12-13-2006, 03:42 PM
I have the following code.
When I have multiple checkboxes the following code works fine. But if I have only one check box, then I even when I check that one checkbox I get the alert as " check atleast one checkbox". I am having this situation because sometimes I have multiple but other times I only have one checkbox (comes from backend code). How to fix this?
When I have only one checkbox, when the user checks it I want it to open a new window. But it still gives the alert
<html>
<head>
<title>Untitled</title>
<script Language="JavaScript">
<!--
function validate_checkbox()
{
var checkboxSelect = 0;
for (counter = 0; counter < window.document.forms["frmSpecs"].Check1.length; counter++)
{
if (window.document.forms["frmSpecs"].Check1[counter].checked)
{
checkboxSelect = checkboxSelect + 1;
}
}
if (checkboxSelect > 3 )
{
alert("You are limited to only three selections")
return (false);
}
if (checkboxSelect == 0 )
{
alert("Please select atleast one")
return (false);
}
return true;
}
-->
</script>
</head>
<body>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="10"><img src="../common/images/spacer.gif" height="1" width="10" alt="" border=0/></td>
<td valign="top">
<form name="frmSpecs" id="CategorySpecs" method="post">
<input type="checkbox" name="Check1" value="1">word1<br>
<a href='#' onclick ='javascript:validate_checkbox();returnValue:false;'><img src='images/img1.gif' border='0'></a>
</form>
</td>
</tr>
</table></body>
</html>
When I have multiple checkboxes the following code works fine. But if I have only one check box, then I even when I check that one checkbox I get the alert as " check atleast one checkbox". I am having this situation because sometimes I have multiple but other times I only have one checkbox (comes from backend code). How to fix this?
When I have only one checkbox, when the user checks it I want it to open a new window. But it still gives the alert
<html>
<head>
<title>Untitled</title>
<script Language="JavaScript">
<!--
function validate_checkbox()
{
var checkboxSelect = 0;
for (counter = 0; counter < window.document.forms["frmSpecs"].Check1.length; counter++)
{
if (window.document.forms["frmSpecs"].Check1[counter].checked)
{
checkboxSelect = checkboxSelect + 1;
}
}
if (checkboxSelect > 3 )
{
alert("You are limited to only three selections")
return (false);
}
if (checkboxSelect == 0 )
{
alert("Please select atleast one")
return (false);
}
return true;
}
-->
</script>
</head>
<body>
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
<td width="10"><img src="../common/images/spacer.gif" height="1" width="10" alt="" border=0/></td>
<td valign="top">
<form name="frmSpecs" id="CategorySpecs" method="post">
<input type="checkbox" name="Check1" value="1">word1<br>
<a href='#' onclick ='javascript:validate_checkbox();returnValue:false;'><img src='images/img1.gif' border='0'></a>
</form>
</td>
</tr>
</table></body>
</html>