PDA

Click to See Complete Forum and Search --> : form validation using an array


pnj
Oct 30th, 2000, 12:13 PM
I asked this in VBscript forum, I guess I should have asked it here.
I have a form w/ 15 text box's.
four of those text box's are part of an address,it is not a required part of the form but I want to see if the users fills out any part of the address then alert them to fill out all fields.

I can do this w/ long hand like this:

if
(document.form.address.value!=0 &&
document.form.city.value ==0 &&
document.form.zip.value ==0)
{
alert("fill out the city please")
document.form.city.focus();
return false;
}

but I would like to use an array and loop through the fields in that manner.

any suggestions?

thanks

ShIzO
Oct 31st, 2000, 09:10 AM
sorry but you can't use control arrays just like you use them in VB. gotta use individual objects.

Nov 4th, 2000, 10:13 PM
sorry, but you can't NOT have arrays "just like you use them in VB". A web page is nothing but arrays and arrays of arrays, that's one of the great features about them.

I've put together a stupid page demonstrating object arrays here:http://www.panteravb.com/data/objectarrays.asp. Just right click to view source.

As far as controls arrays go, they're inherent in javascript. if a form only has one element, it's the 0th element in the forms elements array.

My advice on the address, name all of the address fields the same name, if there are 4 elements in the address then maybe loop through each. Set an empty flag(a variable called bEmpty set to true) and while looping through the address field array if there is a value in any field flip the flag to false; then recheck the address array if the flag is set to false(indicating one field was not empty) and you hit a field that IS empty then alert the user that if any part of the address is filled in then the whole thing must be filled in. I dunno, just thinkin off the top of my head, but the point is that you have arrays at your disposal.