Link to home
Start Free TrialLog in
Avatar of ldbkutty
ldbkuttyFlag for India

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of archrajan
archrajan

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;
}
Avatar of ldbkutty

ASKER

thanks and welcome back (to form) :=)

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