?
Solved

Checkbox Array problem..

Posted on 2005-03-25
4
Medium Priority
?
20,446 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:ldbkutty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
archrajan earned 1000 total points
ID: 13633152
<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
 
LVL 25

Expert Comment

by:archrajan
ID: 13633170
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
 
LVL 32

Author Comment

by:ldbkutty
ID: 13633198
thanks and welcome back (to form) :=)

I opted first one as it seems faster.
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13633205
thanks kutty!
pride in answering ur questiion..

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question