Click to See Complete Forum and Search --> : element lengths in forms


anguilla
02-03-2003, 02:28 PM
Hello,

Need help. I'm writing javascript function to walk down a entire form, and I'm checking various element types to make sure the fields are filled in. But when I get to radio buttons, I'm having problems figuring out how many elements correspond to that radio button. For example,

str = myform.elements

for ( i = 0; i < str.length; i++ ) {

if ( str[i].type == 'radio' ) {
alert 'length = ' + str[i].name.length).
}
}

Well, the alert is printing out 11 for the length (where it should be 2). I'm guessing it's taking how many characters str[i].name has, which is what I don't want. Is this some sort of evaluation problem ??? Are there any ways around this ??

Thank you !!

gil davis
02-03-2003, 03:19 PM
Posting your form would help give you an answer.

If you think that str[i].name is a group of radio button names, then you deserve any problems you have, since "name" already has a meaning in forms.

You could try using the for ... in syntax to see everything in the form structure:

for (var i in str)
{alert(i + " = " + str[i]);
for (j in str[i])
{alert(j + " = " + str[i][j]);}
}

anguilla
02-03-2003, 03:31 PM
str[i].name is not the name of the radio buttons. The name of the radio is called NumberOfDevs, I was just using the .name property to print out the name of the form element. Again, I was walking down all elements in the form. I'm trying to write a generic parsing function that gets the name of the form passed as a variable, and I'm just checking to make sure not fields are blank. I have everything working except for radio buttons. I think I have some sort of evaluation problem. Any more thoughts ??
Thanks.

gil davis
02-03-2003, 07:52 PM
Like I said before:Posting your form would help give you an answer.
alert('length = ' + str[i].name.length);
will definitely give you the number of characters in the name of the element, whereas

alert('length = ' + str[i].length);

will give you the number of elements (radio buttons in your case).

Dan Drillich
02-03-2003, 09:23 PM
Please have a look at this example -


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example</title>
</head>
<body>
<script type="text/javascript">
<!--


function isRadioEmpty(n) {

var grp = eval(n);
var j, len = grp.length;
var found = false;


for (j=0; j<len; j++) {
if (grp[j].checked) found = true;
}



return ! found;

}


function verify() {


var frm = document.buttons;
var len = frm.length;


for(x=0;x<len;x++)
{
if(frm[x].type=="radio")
{
var radioName = frm[x].name;

}
}


var emptyRadio = isRadioEmpty("document.buttons."+radioName);

return ! emptyRadio;

}
// -->
</script>

VAT Registered
<br>
<form name="buttons" action="http://www.iwon.com" onsubmit="return verify();" >
Yes
<input type="radio" name="VAT" value="a1">
No
<input type="radio" name="VAT" value="a2">
<br>
x - <input type="text" name="x" value="">
<br>
<input type="submit">

</form>



</body>
</html>


Good night!
Dan