Iterate through the form's elements array, check if each one is a [checkbox / radio / other] and then check the name for the element against a regex. If it matches then you need to use this element to validate eg
<script language="javascript1.2" type="text/javascript"> function validate_form() { valid=true; df=document.forms["myform"]; var regex="del\[\d+\]"; for (i=0; i<df.elements.length) { if (df[i].type=="checkbox" && df[i].name.match(regex)) { if (df[i].checked=true) { valid = false; break; } } } } </script>
So you're checking to see if each element is a checkbox form type, then if it matches the field name you want to validate against (there may be more than one set of checkboxes with different array names), and finally you're seeing if it's checked.
Since there's no point carrying on with the loop if we've failed one, we then use break; to jump out of the loop rather than bash any other form elements. The odd 'escapes' in the regex before the [ and ] are because [ and ] are used in regular expressions to denote character classes eg [0-9*£$] or [a-zA-Z0-9] and so on. The \d+ denotes 'any digits' and is a shorthand way to write [0-9]
Cheers - Neil
At 09:46 16/11/2004 +0000, you wrote:
Message-Id: <200411160303.iAG332B7024093@xxxxxxxxxxxxxxxxxxxxxxxxxx> From: "Chris Payne" <cjp@xxxxxxxxxxxxxxxxx> To: <php-db@xxxxxxxxxxxxx> Date: Mon, 15 Nov 2004 22:03:10 -0500 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0000_01C4CB5E.E5407E70" Subject: PHP / Javascript question
Hi there everyone,
I have a form with check boxes and with PHP I use the array feature for the form name. Now I need to check if the tickboxes are ticked and if not return an error, normally with javascript I would use:
<script language="JavaScript"> function validate_form ( ) { valid = true; if ( document.removeitems.del.value == "" ) { alert ( "You cannot remove an item if it is not selected." ); valid = false; } return valid; } </script>
BUT because the tickboxes information is stored in a PHP Array called del[] I am having major problems getting it to work. Does anyone know how to determine with javascript if any boxes are ticked, when the name for the box is an Array such as del[] ??? I?m grabbing the data from my MySQL DB without a hitch etc ?. And I know technically this is more MySQL / PHP list, but it is related and I?ve looked online and can?t seem to find anything, so thought I?d try here as usually you are all very helpful.
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php