hi,
i want to validate checkboxes where each checkbox has a different name
1.atleast one checkbox should be select.
2.not more than 4 checkboxes should be selected.
can any one tell me how to validate this.

Try following code

<html>
<script lang='javascript'>

var checkedcount=0;

function validate()
{
	if (checkedcount<=0 || checkedcount>4 )
	{
	 	alert('Select one to four options!');
		return false;
	}
}

function checkinfo(chkbox)
{
	
	if(chkbox.checked==true)
		checkedcount++;
	else
		checkedcount--;

}

</script>
<body>	
	<form name=frm action=some.php method=post>
	<input type=checkbox id=chk[0]   name=chk[0] value=0 onclick='javascript:checkinfo(this);' > Option 0<br>
	<input type=checkbox id=chk[1]   name=chk[1] value=1 onclick='javascript:checkinfo(this);' > Option 0<br>
	<input type=checkbox id=chk[2]   name=chk[2] value=2 onclick='javascript:checkinfo(this);' > Option 0<br>
	<input type=checkbox id=chk[3]   name=chk[3] value=3 onclick='javascript:checkinfo(this);' > Option 0<br>
	<input type=checkbox id=chk[4]   name=chk[4] value=4 onclick='javascript:checkinfo(this);' > Option 0<br>
	<input type=checkbox id=chk[5]   name=chk[5] value=5 onclick='javascript:checkinfo(this);' > Option 0<br>

                   <input type="submit" name="Submit" value="Next" class="button" onClick="return validate();">


	</form>
	
</body>
</html>

i want the user to restrict by allowing him to select only 4 checkboxes.the checkboxes have a different name.

try it please

<html>
<script lang='javascript'>

var count=0;
function check(mychkbox)
{
       if(mychkbox.checked==true)
		count++;
	else
		count--;

	if(count<=0)
	{
	alert('Must select one options!');
	mychkbox.checked=true;
        return false;
	}
	else if(count>3)
	{
	alert('You can maximum Select four options!');
	mychkbox.checked=false;
	return false;
	}
}
</script>
<body>	
<input type=checkbox name="chkbox1" value="1" onclick="check(this)" checked="checked"  > first<br>
<input type=checkbox name="chkbox2" value="2" onclick="check(this)"  > second<br>
<input type=checkbox  name="chkbox3" value="3" onclick="check(this)"  > third<br>
<input type=checkbox  name="chkbox4" value="4" onclick="check(this)"  > fourth<br>
<input type=checkbox  name="chkbox5" value="5" onclick="check(this)"  > fifth<br>
<input type=checkbox  name="chkbox6" value="6" onclick="check(this)"  > sixth<br>

	
</body>
</html>

that will be more effective
try it please

<html>
<script lang='javascript'>

var count=0;
function check(mychkbox)
{
	
	if(mychkbox.checked==true)
	{	count++; }
	else
	{	if(count>0){count--;} }
	
	if(count<=0)
	{
	
	alert('Must select one options!'+count);
	mychkbox.checked=true;
	return false;
	}
	else if(count>3)
	{
	alert('You can maximum Select four options!'+count);
	mychkbox.checked=false;
	return false;
	}
}

</script>
<body>	

	<input type=checkbox id="chkbox1"   name="chkbox1" value="1" onclick="check(this)" checked="checked"  > first<br>
	<input type=checkbox id="chkbox2"   name="chkbox2" value="2" onclick="check(this)"  > second<br>
	<input type=checkbox id="chkbox3"   name="chkbox3" value="3" onclick="check(this)"  > third<br>
	<input type=checkbox id="chkbox4"   name="chkbox4" value="4" onclick="check(this)"  > fourth<br>
	<input type=checkbox id="chkbox5"   name="chkbox5" value="5" onclick="check(this)"  > fifth<br>
	<input type=checkbox id="chkbox6"   name="chkbox6" value="6" onclick="check(this)"  > sisth<br>

	
</body>
</html>

if you dont want even single check box checked by default then you should use this code

<html>
<script type="text/javascript" language="javascript">

var count=0;



function check(mychkbox)
{
	if(mychkbox.checked==true)
	{	count++; }
	else
	{	if(count>0){count--;} }
		
	if(count>4)
	{
	alert('You can maximum Select four options!');
	mychkbox.checked=false;
	return false;
	}
}
function validate()
{
	if(document.myfrm.chkbox1.checked==false && document.myfrm.chkbox2.checked==false && document.myfrm.chkbox3.checked==false && document.myfrm.chkbox4.checked==false && document.myfrm.chkbox5.checked==false && document.myfrm.chkbox6.checked==false)
	{
	alert('Must select one options!');
	mychkbox1.checked=true;
	return false;
	}
}


</script>
<body>	
	<form name="myfrm" id="myfrm" action="" method="post">
	
	<input type=checkbox id="chkbox1"   name="chkbox1" value="1" onclick="check(this)"  > first<br>
	<input type=checkbox id="chkbox2"   name="chkbox2" value="2" onclick="check(this)"  > second<br>
	<input type=checkbox id="chkbox3"   name="chkbox3" value="3" onclick="check(this)"  > third<br>
	<input type=checkbox id="chkbox4"   name="chkbox4" value="4" onclick="check(this)"  > fourth<br>
	<input type=checkbox id="chkbox5"   name="chkbox5" value="5" onclick="check(this)"  > fifth<br>
	<input type=checkbox id="chkbox6"   name="chkbox6" value="6" onclick="check(this)"  > sisth<br><br><br>
	
	<input type="button" name="Save" value="Save"  onclick="validate();"/>

	
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.