Click to See Complete Forum and Search --> : Incrementing variable names in loop??


WolfShade
09-22-2003, 10:32 PM
Can it be done?

What I have is a form that is currently static; it will eventually be dynamically generated with tabs that can be anywhere from 1 to 100 tabs, using <DIV id="contentblockx"> surrounding tables that will provide different fields depending upon what tab is clicked. Since the number of tabs will be dynamic, I need to create dynamic form validation.

In normal validation, I create a variable called warn and set it to equal "", then if anything erroneous happens, warn += "blah blah blah"; if warn !="" then alert(warn) else return true.

I'd like to have as many warn variables as there are tabs (warn1 through warn[x]). Unfortunately, I can't figure out how to establish a warn[x] in the for loop.

for (a=1; a<numberOfTabs+1; a++) {
warn[a]="";

-= validation stuff goes here =-
}

.. isn't working. Even putting it within an eval() didn't do it. Any suggestions? I've been searching a lot and have not yet found anyone else asking this question (yet), nor any kind of tutorial on how to do it. Any suggestions?

Thank you for your time.

Khalid Ali
09-22-2003, 10:48 PM
if I understood you correctly????

in the loop do something along these lines
if(there is an error){
warn[x]="blah blah&lt;br&gt;";
}

and once all is done you check to see if there is data in warn
if(warn.length>0){
//run a loop to print errors
}

WolfShade
09-22-2003, 11:19 PM
I love it when I figure something out that's been kicking my butt for hours. I also hate it when I figure it out AFTER I've posted a question to a forum.

Khalid, thanks for your suggestion. I think it may be similar to what I've done. I've tested it, and it works great. Now, I read your sticky about placing script under the PHP headers; I've never done it before, but I'll test it, here. The following is what finally succeeded:


function checkData() {
warn = "";
df = document.customize;
warnb = new Array(numberOfTabs+1);
warnc = 0;

for (a=1; a<numberOfTabs+1; a++) {

warnb[a] = "";
if (eval("df.TabType"+a+".options[df.TabType"+a+".selectedIndex].value") == 0) {
warnb[a] = "Description of ERROR\n";
}

if (warnb[a] != "") {
warnb[a] = " ----- Tab " + a + " Validation Issue -----\n" + warnb[a];
warnc = 1;
}
warn = warn + warnb[a];
}
if (warnc == 0) {
return true;
}
else {
alert('* * * PLEASE MAKE THE FOLLOWING CORRECTIONS * * *\n_______________________________________________\n\n' + warn);
return false;
}
}


Now, granted, the selectedIndex could just as easily have just been left to selectedIndex as opposed to options.value, but I included that just in case the select value is not the same as the selectedIndex (like an actual word.)

Thanks!

Khalid Ali
09-23-2003, 08:15 AM
youare welcome....;)