hello,

I m trying to check if user has selected at least one checkbox and give an alert for select atleast one.
the checkbox in the forms are dynamics depends up on data received from database
for ex.
user has to select at least one customer to process ahead .
i have written below code to validate :

alert_flag=0;
	for(j=0;j<count0;j++)
	{	
		//alert(document.frm.elements[j].name);
		//alert("cust_id["+i+"]");
		if(document.frm.elements[j].name=="cust_id["+i+"]") 
		{
			
			i++;
			if(document.frm.elements[j].checked)
			{
				alert_flag=1;
				break;
			}
		}
	}
	if(alert_flag==0)
	{	
		alert("Please select At least Customer ");//At least one record must be checked for delete
		return false;
	}

j counter check for all the element and i counter increase only when checkbox found.
this doesn't work sometime Though user has checked one of them.It gives the alert "Please select At least Customer"
don't know how but it not happens all the time.
i have displayed a list of all customer in page so there can be more elements.

You try this code in separate page, then try to do in ur logic.

<?php 
//find out how many check box u have from database,
 $noofcheckboxes =5 ;

?>

<script lang='javascript'>
	//on page load we assume that 0 boxes checked
	var checkedcount=0;
	
	//this fuction is called when any box is checked or unchecked, it wil update checkedcount variable
	function checkinfo(chkbox)
	{
			
			if(chkbox.checked==true)
				checkedcount++;
			else
				checkedcount--;
	
	}
	     
	//this fucntion is called when form is submitted
	function checkdata()
	{
		
		if(checkedcount==0)
		{	
			alert("Please select At least Customer ");//At least one record must be checked for delete
			return false;
		}
		alert("ok");//
	}
</script>

<body >
<form>

<input type=checkbox id=cust_id[0]   name=cust_id[0] value=c1 onclick='javascript:checkinfo(this);' > cust 1<br>
<input type=checkbox id=cust_id[1]   name=cust_id[1] value=c2 onclick='javascript:checkinfo(this);' > cust 2<br>
<input type=checkbox id=cust_id[2]   name=cust_id[2] value=c3 onclick='javascript:checkinfo(this);' > cust 3<br>
<input type=checkbox id=cust_id[3]   name=cust_id[3] value=c4 onclick='javascript:checkinfo(this);' > cust 4<br>
<input type=checkbox id=cust_id[4]   name=cust_id[4] value=c5 onclick='javascript:checkinfo(this);' > cust 5<br>

<br>
<input type=button onclick='checkdata()' value=check >
<br>

</form>
</body>

thanks for your reply.
i don't want to check on onclick i want to validate when user click on save button.

it your choice when u call, I have just shown demo. U run it separately and check it works or no

hey sorry for my earlier post.
i have applied your solution to my code its great and working fine also.
running code without "for loop" seems gr8.
thank you very much for your solution.

This little function will do your job

function ifChecked(form){for(x in form.elements)if(form.elements[x].checked)return!0}

"Please select At least [1] Customer"
Asserts and implies that the client is a complete retard or it at least is an ugly redundant overhead for both parties.
Nobody will hit "delete" before deciding what to delete first.

And if one does, nothing will happen logically (the function will cancel the action) and one will say "ups I wanted to delete nothing at all -therefore nothing happened. Good, let me select this one..."

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.