Checkbox Array problem..

This example works :

<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
function atLeastOne(){
  var objForm = document.forms[0];
  for(i=0;i<4;i++){
    if(objForm.elements["frm_chk_delete[" + i + "]"].checked){
       return true;
    }
  }
  alert("Please check at least one box!");
  return false;
}
</SCRIPT>
</HEAD>
<BODY>
<form name="frmYourForm" action="yourpage.htm" onSubmit="return atLeastOne()">
<INPUT type="checkbox" name="frm_chk_delete[0]"><br>
<INPUT type="checkbox" name="frm_chk_delete[1]"><br>
<INPUT type="checkbox" name="frm_chk_delete[2]"><br>
<INPUT type="checkbox" name="frm_chk_delete[3]"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</FORM>
</BODY>
</HTML>

but I have the checkboxes like this (within php) :

<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>

How should the validation be done?

PS: I dont want to change the element name and if possible, dont want to add "id" also. The above is just an example for my problem. Original form has various checkboxes with numerous fields.
LVL 32
ldbkuttyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

archrajanCommented:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
function atLeastOne(){
  var objForm = document.forms[0];
  var el = document.getElementsByName('frm_chk_delete[]')
  for(i=0;i<el.length;i++){
    if(el[i].checked){
       return true;
    }
  }
  alert("Please check at least one box!");
  return false;
}
</SCRIPT>
</HEAD>
<BODY>
<form name="frmYourForm" action="yourpage.htm" onSubmit="return atLeastOne()">
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="checkbox" name="frm_chk_delete[]"><br>
<INPUT type="submit" value="Submit" id=submit1 name=submit1>
</FORM>
</BODY>
</HTML>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
archrajanCommented:
or just this

function atLeastOne(){
  var objForm = document.forms[0].elements
 
  for(i=0;i<objForm.length;i++){
    if(objForm[i].type == "checkbox" && objForm[i].name.indexOf('frm_chk_delete[]')!= "-1" && objForm[i].checked){
       return true;
    }
  }
  alert("Please check at least one box!");
  return false;
}
0
ldbkuttyAuthor Commented:
thanks and welcome back (to form) :=)

I opted first one as it seems faster.
0
archrajanCommented:
thanks kutty!
pride in answering ur questiion..

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.