Click to See Complete Forum and Search --> : validating an array field
Harman
07-16-2004, 03:44 PM
Hi!
i have a form which has an array field
php code***********
while($row = mysql_fetch_object(...){
$tbl .= '<input name="qty['.$x.']" type="text" size="4" maxlength="4"/>';
...
}
when i try to do something like this, it doesnt like it
javascript code ***************
for(i=0;i<total_rows; i++){
...
frm_name.qty[i].value
...
}
I would like to validate it on submit to check at least one of then has a value and and that the values entered are numeric...thanks
steelersfan88
07-16-2004, 04:15 PM
That's because that isn't an array. It is a bunch of fields with []s in the name. Just name each field qty, like:$tbl .= '<input name="qty" type="text" size="4" maxlength="4"/>';Then when referring to them, you access each element in order, as an HTML collection.
Dr. Script
Harman
07-16-2004, 04:21 PM
if i do that itll mess up my php code, php sees it as an array, i dont understand y javascrit cant.
steelersfan88
07-16-2004, 04:32 PM
It isn't an array.<... name="field[0]" ...>
<... name="field[1]" ...>
<... name="field[2]" ...>
<... name="field[3]" ...>
<... name="field[4]" ...>
<... name="field[5]" ...>That's 6 differently named elements that do not go by one name.
Array = a group/series of objects all of the same name, data type, and size/dimension
The above example has 5 different names. Making them the same name makes in an array (really an HTML objects collection). Your other alternative is to use the elements array of the form. If these are the first couple of elements, they can be reached by:document.forms['form_name'].elements[0]
document.forms['form_name'].elements[1]
document.forms['form_name'].elements[2]
document.forms['form_name'].elements[3]
document.forms['form_name'].elements[4]
document.forms['form_name'].elements[5]Then the names you have wouldn't be a problem. Of course, if the first element is a hidden field, and the first qty element is the second form element, it would be [1-6], not [0-5].
Dr. Script
Harman
07-16-2004, 05:18 PM
thanks for your help, i just validated it in php...can you suggest a good tutorial on javascript or a refrence book, i would appriatiate it....thanks again