Click to See Complete Forum and Search --> : How can i validate arrays in Forms?!


hansmaier
12-22-2003, 08:03 AM
how do i validate this line?:

<input name="anzahl_vvk[6]" type="text" size="10" value="0">


this doesn't work:
if (anzahl_vvk[6].value == ""){ .... }

olerag
12-22-2003, 08:17 AM
1. This isn't an array; its a "text" object.
2. Not sure about putting brackets around the "name"
of an HTML form object? Might be alright though.
3. To verify if the field contains a value try..
if (document.form[0].myField.length > 0)

hansmaier
12-22-2003, 08:23 AM
i need the brackets for the php-script, thats the problem!

i can't use your 3. because i don't know how many form-fields i will have (the are created dynamicaly).

please help!

olerag
12-22-2003, 08:45 AM
If the brackets are permissible, that's fine.

You can always configure your form so the "text" items you
want to check for NULLs "begin" with a specific naming
convention (unlike your other objects). Then, as you loop
thru your form, only check for "text" items that "begin" with
your naming convention. Such as...


var form = document.forms[0];
for (var i=0; i<form.elements.length; i++) {
if (form.elements[i].type == "text") {
if (form.elements[i].name.substring(0,5) == "check") {
if (form.elements[i].value.length > 0) {


This assumes you've given your text items you want to check
a name that commences with "check".

fredmv
12-22-2003, 06:09 PM
if(document.forms[0]['anzahl_vvk[6]'].value == '')
{
// ...
}