Click to See Complete Forum and Search --> : how tell if i have one field or array
digiti0
06-14-2003, 08:22 PM
I'm building a page in ASP and validating the input fields in javascript before submitting it back to the server. Some fields usually come out multiple times so I'm using the field's .length property to loop through them. Sometimes they only come out once, so the .length property describes the actual length of the field. Is there another property that I can look at to tell if there is an array of fields or just one?
Thanks!
Khalid Ali
06-14-2003, 08:29 PM
Something on these lines might work.
fiiledName[1]!=null
if fieldName is an array and it has more then 1 values it will not be null otherwise it will be..
sneaky eh..
:D
digiti0
06-14-2003, 08:59 PM
Thank you--that was a good idea, but for some reason, on a non-array field, the check for nulls failed in both [0] and [1]--which is kind of weird! Oh well
Khalid Ali
06-14-2003, 09:10 PM
Actually I just got a better idea and it will work
suppose your text fields have name
textfields
and you want to know if this filed is only one or more then one( an array)
if(isNaN(textfields.length)){
//one filed no array
}else{
//array
}
digiti0
06-14-2003, 09:23 PM
Didn't work--I get some kind of value in .length either way, it's just that I can't use it if there isn't an array of fields. I guess I'm going to have to create a bunch of hidden fields when I build the page, indicating whether I'm dealing with arrays or not.
Thanks for being there though, it's nice to know I'm not banging my head against the wall by myself.
Khalid Ali
06-14-2003, 09:25 PM
Originally posted by digiti0
Didn't work--
What are you talking about ...:D
I tested it in both NS6+ and IE6+ browsers...it worked perfectly...I guess you could not implement the solution...
digiti0
06-16-2003, 08:00 AM
I'm still getting a value of 3 in length if there's only one occurrence of the field. Maybe because my field is defined as <select ....
<option
<option...
and not
<input type=text....
(3 is the length of my longest <option>)
???
digiti0
06-16-2003, 10:23 AM
yep, that was why...when the field is a select, .length = the number of options in the select, so I had to check the length property of the first option. The following code works. Thanks for your help Khalid and Dave
if (document.myform.fieldname[0].length) {
counter = document.myform.fieldname.length
alert("its an array" + counter)
for (i = 0; i < counter; i++) {
if (document.myform.fieldname[i].value == " "){
alert("Please indicate choice: Yes/No.")
document.myform.fieldname[i].focus()
return false
}
}
}
else {
alert("its not an array")
if (document.myform.fieldname.value == " "){
alert("Please indicate choice: Yes/No.")
document.myform.fieldname.focus()
return false
}
}
Khalid Ali
06-16-2003, 10:27 AM
good...:cool: