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("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.

Recommended Answers

All 5 Replies

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

//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)
	//this fucntion is called when form is submitted
	function checkdata()
			alert("Please select At least Customer ");//At least one record must be checked for delete
			return false;

<body >

<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>

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


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, learning, and sharing knowledge.