Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Checkbox Array problem..

Posted on 2005-03-25
4
Medium Priority
?
20,468 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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

577 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