[Javascript] Loop problem
David T. Lovering
dlovering at gazos.com
Tue Mar 18 07:43:28 CST 2003
Well, I see a couple of problems...
In the loop 'for (i=1;i=<%=Numfrm%>;i++)' you have two equivalence assignments, namely
i=1 and i=<%=Numfrm%>, where <%Numfrm%> is some variable derived elsewhere. (Incidently,
I hope this isn't the real syntax you are using, as <%Numfrm%> will blow up the syntax
parser inside JavaScript). If the number of fields is variable and externally defined, a
much better way of passing it into the function is as follows:
var Numfrm, form;
function checkFrm(Numfrm, form) {
var why ="";
for (var i=1;i<Numfrm;i++) { // it is good practice to pre-declare floating indices as 'var'
why = checkName(document.form.element[i].value); // You don't really want to concatenate why //
if ( why != "") {
alert(why);
return false;
}
}
} // Note that your example was missing a closing brace, and 'form1' wasn't defined //
I'm only making a wild-ass guess that what you are doing is looking at all the text fields in a form 'form'
(and they'd better BE text fields, or else the ".value" declaration will crap out), passing them into checkName,
and if anything other than a null string squirts out, you exit with a false condition. Incidently, (I'm sure
you knew this), you don't have a matching 'return true' anywhere in your code.
The assignment you use, namely 'why += ...' will tack the previous value of the why string onto the front of
whatever comes back from your checking routine. Since it dies whenever this is non-null, the concatenation
is no different than a straight assignment.
The checking routine is nearly correct, but could do with a minor bit of tweaking:
var strng;
function checkName (strng) {
var return_string = "";
//test of the filling
if (strng == "") {
return_string = "you didn't enter a username. \n";
}
return return_string;
} // Note -- 'error' is a reserved token, and shouldn't be used outside its correct context
This should work more along the lines you expected.
-- Dave Lovering
Alexandre Zglav wrote:
>
> Hello all.
>
> I quite new to javascript and I have a problem with a loop and I just cant
> find a solution to it on the internet.
>
> I am trying to validate a for m with a variable amount of fields.
>
> Here is the code i am using :
>
> <!--
>
> function CheckForm(form1) {
>
> var why ="";
>
> for (i=1;i=<%=Numfrm%>;i++)
> why += checkName(form1.name[i].value);
> if ( why != "") {
> alert(why);
> return false;
> }
>
> }
>
> function checkName (strng) {
> var error = "";
>
> //test of the filling
> if (strng == "") {
> error = "you didn' enter a username. \n";
> }
> return error;
> }
>
>
> -->
>
> Note that <%=Numfrm%> is a the variable i am retrieving from ASP. It
> returns me an integer value chosen in an earlier field.
>
> Now my problem is at this point :
>
> why += checkName(form1.name[i].value);
>
> It seems that [i] is not accepted as I always get an error at this level
> in my script debugger.
>
> Can someone help me with this ?
>
> Thanks in advance.
>
> Best regards.
> _____________________________________
> IT Projects
> Alexandre Zglav
> Heritage Finance and Trust Company
> 12 cours des bastions
> 1205 Geneva
> Switzerland
> Phone : ++ 41 22 817 31 68
> ----------------------------------------------------------------
> This document should only be read by those persons to whom it is
> addressed and is not intended to be relied upon by any person
> without subsequent written confirmation of its contents. If you
> have received this e-mail message in error, please destroy it
> and delete it from your computer.
> Any form of reproduction, dissemination, copying, disclosure,
> modification, distribution and/or publication of this E-mail
> message is strictly prohibited.
> ----------------------------------------------------------------
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dlovering.vcf
Type: text/x-vcard
Size: 304 bytes
Desc: Card for David T. Lovering
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20030318/e1d7904e/attachment.vcf>
More information about the Javascript
mailing list