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.

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?

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.