Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox array validation

Status
Not open for further replies.

anderzreevez

Programmer
Jul 20, 2004
7
GB
Hi,

I cannot work out how to validate the checkbox array 'leadcontacts[]' as having any of its array elements containing a check?

Basically there is an ID associated with an array of checkboxes all named leadcontacts[]. However standard javascript validations such as that below cannot accept checkbox ARRAYS?

Any ideas?

Anders

Example function:

<script language="JavaScript">
<!--
function validate_form ( )
{
if ( document.form1.leadcontacts[].checked == false )
{
alert ( "Please select at least one contact to assign to this lead." );
valid = false;
}
}
//-->
</script>
 
The problem is in the way that you're accessing the form element. Javascript doesn't know how to accept the array of checkboxes when you define it this way:
document.form1.leadcontacts[].checked

Use this example to give you an idea of how to check the array to see if something's been checked:
Code:
<script language="JavaScript">

function validateForm() {
   obj = document.form1.elements("leadcontacts[]");
   for (i = 0; i < obj.length; i++) {
      if (obj[i].checked) {
         alert("you're fine");
         return true;
      }
   }
   alert("need to check something");
   return false;
}

</script>
<body>
<form name=form1>
<input type=checkbox name="leadcontacts[]">blah1<br>
<input type=checkbox name="leadcontacts[]">blah2<br>
<input type=checkbox name="leadcontacts[]">blah3<br>
<input type=button value='click me' onclick='validateForm()'>
</form>
</body>

-kaht

banghead.gif
 
Thanks for that...

If works great if the array has more than 2 elements/value stored within it (i.e the HTML outputs more than 1 checkbox) but when only one is onscreen it fails to do the check and allows the submit to go through without doing the negative alert.

Any ideas?
 
this will tell u if there is more than one checkbox in a page:
obj = document.form1.elements("leadcontacts[]");
if(obj.length)
{
more than one box
}
else
{
//one box
obj.checked=false
}

Known is handfull, Unknown is worldfull
 

Incidentally, you should not be using square brackets as part of your element names:

w3c said:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Hope this helps,
Dan
 
I'm using square brackets so PHP recognise the variable as an array - I know it complicates things!
 

Blimey - I wonder how the PHP authors can justify forcing users to break the HTML 4.x standards. That is absolutely shocking!

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top