0

I'm having a hard time validating a checkbox. How to do it?

This is my checkboxes input

<input type="checkbox" name="topic[]" value="HTML"/>HTML<br/>
<input type="checkbox" name="topic[]" value="XHTML"/>XHTML<br/>
<input type="checkbox" name="topic[]" value="CSS"/>CSS<br/>
<input type="checkbox" name="topic[]" value="JavaScript"/>JavaScript<br/>
<input type="checkbox" name="topic[]" value="XML"/>XML<br/>
<input type="checkbox" name="topic[]" value="PHP"/>PHP<br/>

I would like to check whether the user select at least one of the items using FOR LOOP. I don't need to display the check values. I just want to make the selection mandatory.

Edited by AngelicOne: n/a

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by AngelicOne
0

I've tried this and it works

var checked = false;
for(i=0;i<document.myForm.topic.length;i++)
{
if(document.myForm.topic[i].checked)
{
checked = true;
}
}

if(checked == false)
{
alert("Topic required");
valid = false;
}

The problem is that I couldn't access the checkboxes if the name is explicitly declared as an array, which is "name=topic[]". How could I access it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.