[Javascript] Loop problem

David T. Lovering dlovering at gazos.com
Tue Mar 18 08:09:54 CST 2003


... which is backwards from the way most validation routines operate ...

I think you meant

  for (var i=0;i<document.formsName.length;i++){
    if(document.formsName.elements[i].value == ''){
      alert('Field \'' + document.formsName.elements[i].name + '\' is empty: please enter a value');
    }
  }

In Laurent's original routine we were checking the NAMES of the fields to see if they were null, and since
it is unlikely that any were, this would give very misleading results.  Again, I should note that this code
snippet will ONLY work with text fields, as pull-downs, radio-buttons, etc. are evaluated in quite a different
fashion.  [It doesn't take much to extend this to cover any sort of entry, but it is somewhat more complex as
you have to check the TagName and Type of the element[i] to see what sort of test you need to do].  You also
need to be aware that hidden fields (which are user invariant) will mess this up.


-- Dave Lovering

"Muchacho, Laurent (TWIi London)" wrote:
> 
> Stop everyone
> 
> This is pretty simple
> 
> when you do your loop throw a form you can simply do
> 
> for (var i=0;i<document.formsName.length;i++){
>         if(document.formsName.elements[i].name!=''){
>                 alert(document.formsName.elements[i].name);
>         }
> }
> 
> this will alert the name value of each element in your form are not empty
> 
> Laurent
> 
> ps: this link might help
> http://developer.netscape.com/docs/manuals/js/client/jsref/index.htm
> 
> click on table of content at the top of the main frame
> 
> -----Original Message-----
> From: Chris Tifer [mailto:christ at saeweb.com]
> Sent: 18 March 2003 13:38
> To: javascript at LaTech.edu
> Subject: Re: [Javascript] Loop problem
> 
> >From the looks of it, this would never loop:
> 
> > for (i=1;i=<%=Numfrm%>;i++)
> 
> Should their be some of of comparison, such as:
> 
> <  or <= or > or >= or ==
> 
> When you say " var1 = var2", you're automatically setting
> var1 to var2's values. == checks for equality.  That'd be where
> I started to debug.
> 
> Chris Tifer
> 
> ----- Original Message -----
> From: "Alexandre Zglav" <azglav at heritage.ch>
> To: <javascript at LaTech.edu>
> Sent: Tuesday, March 18, 2003 7:44 AM
> Subject: [Javascript] Loop problem
> 
> > 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
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> DISCLAIMER - The preceding e-mail message (including any attachments)
> contains information that may be confidential, may be protected by the
> attorney-client or other applicable privileges, or may constitute non-public
> information.  It is intended to be conveyed only to the designated
> recipient(s) named above.  If you are not an intended recipient of this
> message, or have otherwise received it in error, please notify the sender by
> replying to this message and then delete all copies of it from your computer
> system.  Any use, dissemination, distribution, or reproduction of this
> message by unintended recipients is not authorized and may be unlawful. The
> contents of this communication do not necessarily represent the views of
> this company.
> _______________________________________________
> 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/0459d2d2/attachment.vcf>


More information about the Javascript mailing list