yourbudweiser
asked on
Must select at least one checkbox
I have a dynamic form with multiple checkboxes. I need to make sure that at least one checkbox is checked but only when the checkbox is actually unchecked. For example, the user unchecks 4 out of 5 boxes but when he unchecks box 5 an alert appears that one box must be checked and box 5 is left checked until he checks another and then unchecks box 5.
Thanks for the help.
Thanks for the help.
What are the names of those five checkboxes? Are they all same name or different names?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the checkboxes all have different names as I am composing an asp.net page.
basicinstinct:your solution works in an html page but I am having trouble getting it to run in the context of my .net page.
The checkboxes already have an onClick event but I am trying to load yours first.
basicinstinct:your solution works in an html page but I am having trouble getting it to run in the context of my .net page.
The checkboxes already have an onClick event but I am trying to load yours first.
if you already have this:
onclick="doStuff();"
then you should just be able to make it this:
onclick="doStuff(); return checkChecked(this);"
onclick="doStuff();"
then you should just be able to make it this:
onclick="doStuff(); return checkChecked(this);"
otherwise try this, it will append itself to your existing onclick events...
<html>
<head>
<script type="text/javascript">
function init()
{
var frm = document.myform;
var chkboxes = getChkBoxes(frm);
for(var i=0; i<chkboxes.length; i++)
{
if(window.addEventListener )
chkboxes[i].addEventListen er('click' ,proxy,tru e);
else
chkboxes[i].attachEvent('o nclick', proxy);
}
}
function proxy(e)
{
var el;
el = e.target;
if(el != null)
{
if(!checkChecked(el))
{
e.preventDefault(true);
}
}
else
{
el = e.srcElement;
if(!checkChecked(el))
{
e.returnValue = false;
}
}
}
function checkChecked(chkbox)
{
var chkboxes = getChkBoxes(chkbox.form);
var foundChecked = false;
for(var i=0; i<chkboxes.length; i++)
{
if(chkboxes[i].checked && chkboxes[i] != chkbox)
{
foundChecked = true;
break;
}
}
return foundChecked;
}
function getChkBoxes(ancestor)
{
var rval = new Array();
var inputs = ancestor.getElementsByTagN ame('input ');
for(var i=0; i<inputs.length; i++)
{
if(inputs[i].type != null && inputs[i].type.match(/chec kbox/i))
rval.push(inputs[i]);
}
return rval;
}
</script>
<body onload="init();">
<form name="myform">
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function init()
{
var frm = document.myform;
var chkboxes = getChkBoxes(frm);
for(var i=0; i<chkboxes.length; i++)
{
if(window.addEventListener
chkboxes[i].addEventListen
else
chkboxes[i].attachEvent('o
}
}
function proxy(e)
{
var el;
el = e.target;
if(el != null)
{
if(!checkChecked(el))
{
e.preventDefault(true);
}
}
else
{
el = e.srcElement;
if(!checkChecked(el))
{
e.returnValue = false;
}
}
}
function checkChecked(chkbox)
{
var chkboxes = getChkBoxes(chkbox.form);
var foundChecked = false;
for(var i=0; i<chkboxes.length; i++)
{
if(chkboxes[i].checked && chkboxes[i] != chkbox)
{
foundChecked = true;
break;
}
}
return foundChecked;
}
function getChkBoxes(ancestor)
{
var rval = new Array();
var inputs = ancestor.getElementsByTagN
for(var i=0; i<inputs.length; i++)
{
if(inputs[i].type != null && inputs[i].type.match(/chec
rval.push(inputs[i]);
}
return rval;
}
</script>
<body onload="init();">
<form name="myform">
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
<input type="checkbox" checked="checked"/>
</form>
</body>
</html>
<!--
window.onload = function() {
var chks = document.getElementsByTagN
for(i = 0; i<chks.length; i++){
if(chks[i].type.toUpperCas
}
}
function chkCnt(){
var pass = false;
var chks = document.getElementsByTagN
for(i=0; i<chks.length; i++){
if(chks[i].checked) pass = true;
}
if(!pass){
alert('You must check at least one box!');
this.checked = true;
}
}