Assume in this Example Checkbox generated at run time by JSP,Servlet,ASP etc.
And Before Submiting Form U wan't to assure user selected atleast 1 checkBox
If there is one or more checkbox generated at run time Use this example. Here if there is 1 checkbox its length comes 'undefined', in the below code it is 'undefined' is handled.
<HTML>
<HEAD>
<TITLE>Validate run Time CheckBox</TITLE>
</HEAD>
<script type="text/javascript">
function validate()
{
var len = document.myForm.number.length;
var count = 0;
//len is undefined when there is 1 checkbox
if(len != undefined)
{
//for greater than 1 checkbox
for (i = 0; i < len; i ++)
{
if (document.myForm.number[i].checked)
count++;
}
}
else
{
//for only 1 checkbox
if (document.myForm.number.checked)
count++;
}
if(count == 0)
{
alert("Atleast Select One Check Box");
return false;
}
else
{
alert("Selected "+count);
return true;
}
return false;
}
</script>
<BODY>
<CENTER>
<h3>Run-Time Created Checkbox Validate Example</h3>
<!--
Assume in this Example Checkbox generated at run time by JSP,Servlet,ASP etc.
And Before Submiting Form U wan't to assure user selected atleast 1 checkBox
If there is one or more checkbox generated at run time Use this example
-->
<form action="#" name="myForm" method="post" onsubmit="return validate()">
<TABLE border="1" width="40%">
<tr>
<td>One</td>
<td><input type="checkbox" name="number" value="one"></td>
</tr>
<tr>
<td>Two</td>
<td><input type="checkbox" name="number"value="two"></td>
</tr>
<tr>
<td>Three</td>
<td><input type="checkbox" name="number" value="three"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</TABLE>
</form>
</CENTER>
</BODY>
</HTML>
And Before Submiting Form U wan't to assure user selected atleast 1 checkBox
If there is one or more checkbox generated at run time Use this example. Here if there is 1 checkbox its length comes 'undefined', in the below code it is 'undefined' is handled.
<HTML>
<HEAD>
<TITLE>Validate run Time CheckBox</TITLE>
</HEAD>
<script type="text/javascript">
function validate()
{
var len = document.myForm.number.length;
var count = 0;
//len is undefined when there is 1 checkbox
if(len != undefined)
{
//for greater than 1 checkbox
for (i = 0; i < len; i ++)
{
if (document.myForm.number[i].checked)
count++;
}
}
else
{
//for only 1 checkbox
if (document.myForm.number.checked)
count++;
}
if(count == 0)
{
alert("Atleast Select One Check Box");
return false;
}
else
{
alert("Selected "+count);
return true;
}
return false;
}
</script>
<BODY>
<CENTER>
<h3>Run-Time Created Checkbox Validate Example</h3>
<!--
Assume in this Example Checkbox generated at run time by JSP,Servlet,ASP etc.
And Before Submiting Form U wan't to assure user selected atleast 1 checkBox
If there is one or more checkbox generated at run time Use this example
-->
<form action="#" name="myForm" method="post" onsubmit="return validate()">
<TABLE border="1" width="40%">
<tr>
<td>One</td>
<td><input type="checkbox" name="number" value="one"></td>
</tr>
<tr>
<td>Two</td>
<td><input type="checkbox" name="number"value="two"></td>
</tr>
<tr>
<td>Three</td>
<td><input type="checkbox" name="number" value="three"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</TABLE>
</form>
</CENTER>
</BODY>
</HTML>