Click to See Complete Forum and Search --> : check box doubt


pap
04-16-2007, 06:58 AM
hi,

i have a doubt. how to check whether atleast one checkbox is checked in a checkbox group?
<html>
<head>
<script language="Java Script" type="text/javascript">
function validate(MYFORM)
{
var ischecked=true;
for(var i=0;i<document.MYFORM.mycheckbox.length;i++)
{

if (!document.MYFORM.mycheckbox[i].checked)

}
alert("select one checkbox");

}
</script>
</head>
<body>
<form name="MYFORM" method="post" onSubmit="validate(this)">
<input type="checkbox" name="mycheckbox" value="1">TEST 1<br>
<input type="checkbox" name="mycheckbox" value="2">TEST 2<br>
<input type="checkbox" name="mycheckbox" value="3">TEST 3<br>
<input type="checkbox" name="mycheckbox" value="4">TEST 4<br>
<input type="checkbox" name="mycheckbox" value="5">TEST 5<br>
<input type="submit" value="SUBMIT" >
</form>
</body>
</html>

this is the code i have written. among 5 check boxes we have to select atleast one checkbox.otherwise while submitting the form it has to show one alert box. otherwise if we checked some check boxes it has to go some other page.can anybody suggest me what is the problem in my code?give me reply its urgent.

thank you,
pap

\\.\
04-16-2007, 07:09 AM
What is hapening, you dont explain clearly.

pap
04-16-2007, 07:13 AM
one form will be there.in that one checkbox group will be there.here we have to do the validation through java script to check whether atleast one check box is checked before submitting the data. after clicking the button it has to show an alert if no heck box is selected.

\\.\
04-16-2007, 07:26 AM
here: http://www.w3schools.com/htmldom/prop_checkbox_checked.asp this might help you understand the check boxes further....
and here: http://www.javascriptkit.com/jsref/checkbox.shtml

Also your form lacks "action".

poemind
04-16-2007, 07:40 AM
function validate(MYFORM)
{
var ischecked=true;
for(var i=0;i<document.MYFORM.mycheckbox.length;i++)
{

if (!document.MYFORM.mycheckbox[i].checked)

}
alert("select one checkbox");

}

Hi, it appears to me that once you put an 'action' in your form, you will ALWAYS get the alert. It is outside your for loop and has no logic connected with it. Just off the top of my head, I would keep a count of the number of checked boxes within the loop and then check the count after the loop to see if it is zero. If it is pop the alert.

Hope that helps.

\\.\
04-16-2007, 07:46 AM
the if conditional doesnt change anything, does not change the value of the checked variable.

The second link shows a check box script example if you read it you will find out why it isnt working...

poemind
04-16-2007, 07:56 AM
To be clearer this is what I would do, I think this should work:

function validate(MYFORM)
{
var ischecked=true;
var count=0;
for(var i=0;i<document.MYFORM.mycheckbox.length;i++)
{

if (document.MYFORM.mycheckbox[i].checked)
count++;
}
if(count==0) alert("select one checkbox");

}

\\.\
04-16-2007, 08:11 AM
Or this...
function validate(){
var ischecked=false;
for (i in document.test.checkgroup) ischecked = document.test.checkgroup[i].checked ? true : ischecked;
if(!ischecked) alert('Please Check atleast ONE box');
return ischecked;
}