> case "radio":
> //WHY DOESN"T THIS WORK!!????
> //alert(theForm.elements[i].length);
> alert(i);
That's because elements[i] is not a collection of elements - it is a
single element, so there is no .length property. When you pass
in a field name, it's saying to take into consideration ALL of the
elements with the same name. If that forms a collection, then you
have a .length property.
The code example I have below should help you out:
(copy and paste to test - watch out for wrapping)
================================================
<form name="test">
<input type="checkbox" name="test1" value="1" /><br />
<input type="checkbox" name="test1" value="2" /><br />
<input type="checkbox" name="test1" value="3" /><br />
<input type="checkbox" name="test1" value="4" /><br />
<input type="checkbox" name="test1" value="5" /><br />
<input type="checkbox" name="test1" value="6" /><br /><br />
<input type="checkbox" name="test2" value="1" /><br />
<input type="checkbox" name="test2" value="2" /><br />
<input type="checkbox" name="test2" value="3" /><br />
<input type="checkbox" name="test2" value="4" /><br />
<input type="checkbox" name="test2" value="5" /><br />
<input type="checkbox" name="test2" value="6" /><br /><br />
<input type="checkbox" name="test3" value="1" /><br />
<input type="checkbox" name="test3" value="2" /><br />
<input type="checkbox" name="test3" value="3" /><br />
<input type="checkbox" name="test3" value="4" /><br />
<input type="checkbox" name="test3" value="5" /><br />
<input type="checkbox" name="test3" value="6" /><br />
<input type="button" onclick="loopThrough()" value="Select 'Em">
</form>
<Script language='Javascript'>
<!--
function loopThrough(){
var objForm = document.forms['test']
var arrCheckBoxes = new Array("test1", "test2", "test3")
for(var x = 0; x < arrCheckBoxes.length; x++){
var objEl = objForm.elements[arrCheckBoxes[x]]
for(var y = 0; y < objEl.length; y++){
if(objEl[y].checked){
alert("This box is checked: " + objEl[y].name + " - VALUE: " +
objEl[y].value)
}
}
}
}
// -->
</script>
================================================
So, where I create "arrCheckBoxes", you could instead do:
var arrCheckBoxes = FieldList.split(", ")
or more generically:
var arrMyEl = FieldList.split(", ")
(remember to change any other references from arrCheckBoxes then also)
That'll take your string and put it into an array. From there, you just step
through
each of your elements in the array and validate against any rules you have.
If the above code doesn't help you much, let me know and I'll show you
exactly how to intermingle it with your code to get this working.
Chris Tifer
http://www.emailajoke.com
----- Original Message -----
From: "Paul Broomfield" <[EMAIL PROTECTED]>
To: "ActiveServerPages" <[EMAIL PROTECTED]>
Sent: Thursday, October 17, 2002 6:29 PM
Subject: RE: javascript form element arrays - grrrrr
> Hi,
>
> Heres the code, its called by passing is a list of fields to check for,
> idea being that it'll be called and the fields can be passed dynamically.
>
> Theres a few alerts used for debugging
>
> var theForm = document.forms.ClaimForm;
>
> function CheckForm(FieldList)
> {
> var error = false;
>
> //this works if we use the full name off the form element rather than the
> element array id ie i in the for loop
> alert("this many radios " + theForm.elements["WorkStatus"].length)
>
> for (var i=0; i < theForm.elements.length; i++)
> {
> //see if the form element we are on is one that we want to check -
> passed in FieldList
> if (FieldList.indexOf(theForm.elements[i].name) != -1)
> {
> //we've found a field that we want to check
>
> //now we need to detect its type and process it accordingly
> switch (theForm.elements[i].type)
> {
>
> case "radio":
> //WHY DOESN"T THIS WORK!!????
> //alert(theForm.elements[i].length);
> alert(i);
> if (theForm.elements[i].checked == false)
> {
> error = true;
> alert(theForm.elements[i].name);
> }
> break;
> case "text":
> if (theForm.elements[i].value == '')
> {
> error = true;
> alert(theForm.elements[i].name);
> }
> break;
> case "checkbox":
> if (theForm.elements[i].checked == false)
> {
> error = true;
> alert(theForm.elements[i].name);
> }
> break;
> case "select-one":
> if (theForm.elements[i].value == '')
> {
> error = true;
> alert(theForm.elements[i].name);
> }
> break;
> }
> }
>
> }
>
> alert(error);
>
> }
>
> Paul Broomfield
>
> ---
> You are currently subscribed to activeserverpages as: [EMAIL PROTECTED]
> To unsubscribe send a blank email to
%%email.unsub%%
---
You are currently subscribed to activeserverpages as: [email protected]
To unsubscribe send a blank email to [EMAIL PROTECTED]