[Javascript] Loop problem

Chris Tifer christ at saeweb.com
Tue Mar 18 12:44:44 CST 2003


One more small thing:

>   <form name='myForm' action='javascript:void(null)'
onSubmit='checkForm(document.myForm)'>


If you don't "return" that value in the onSubmit, it doesn't matter if you
return true or false from the function. That form is going to get submitted.

Chris Tifer
http://www.emailajoke.com

----- Original Message -----
From: "David T. Lovering" <dlovering at gazos.com>
To: <javascript at LaTech.edu>
Sent: Tuesday, March 18, 2003 1:20 PM
Subject: Re: [Javascript] Loop problem


>
> Ah ha!  I smell an 'onSubmit' problem:
>
>   You need to modify your original form declaration to include an
'onSubmit' equivalence which points
> to your checking routine (which can be substantially shortened, BTW), i.e;
>
>   <form name='myForm' action='javascript:void(null)'
onSubmit='checkForm(document.myForm)'>
>
>   If you combine all the helpful hints together, checkForm can be reduced
to
>
>   <script language='JavaScript'>
>   <!--
>     var form1;
>     function checkForm(form1) {
>       if (!form) { return false; }
>       if (form == '') { return false; }          // throat-scratch
protection against bogus form
>       if (!document.forms[form1].elements) { return false; }// cure for
empty form syndrome
>       var Numfrm = document.forms[form1].elements.length; // notice that
since 'form1' is a var I don't use ""
>       for (var i=0; i<Numfrm; i++) {
>         if (document.forms[form1].element[i].length == 0) { return
false; }
>       }
>       return true;
>     }
>   // -->
>   </script>
>
>   Notice how 'checkName' got squished into one line of code.  Rule of
thumb: if you are doing something
>   which takes fewer lines of code to do directly than will be required for
a function declaration, don't use a
>   function.  This keeps down the burden on the heap assignments, and
decreases the likelihood of a heap-stack
>   collision -- easily the most common cause of the "blue-screen-of-death"
(BSOD).  Sometimes I violate this
>   rule if a gazillion descending field elements are nested together in the
variable name, but I feel guilty
>   about it afterwards.
>
>   Also, the 'action' declaration I picked can be substituted out for
anything of your choosing -- you'll
>   probably want to point it to your friendly neighborhood CGI/BIN server
which handles your other forms
>   submissions.  However, the onSubmit kicks in BEFORE the action gets
invoked, and if checkForm returns a
>   false, the action is disabled and the form contents don't go anywhere
(in theory).  Sometimes, a broken
>   FrontPage build of the form will send it on anyway, thereby crashing the
forms server and endearing you
>   (and Microsoft) to thousands of freshly outraged customers.  [Hence the
reason I don't use FrontPage].
>
>   Again, I caution you -- if you have ANYTHING in your form which is an
element, but not a vanilla text
>   input, this simple script won't cut it anymore, and you'll have to put
some more hair on its chest.
>
>   -- Dave Lovering
>
>
> Alexandre Zglav wrote:
> >
> > Ok thanks a lot for the tips.
> >
> > I have applied a few changes to my code accordingly to your last email
and
> > the script  doesn't jam anymore. I still have a problem thought : the
> > tests seem to be completely bypassed. If i try my form and leave it
blank,
> > the form is submitted and there is no alert ....
> >
> > Oh and by the way you guessed right : i am trying to test variable
amount
> > of text fields passing them into CheckName
> >
> > Here is the code I am using now :
> >
> >
> >
> > Probably that the return true is missplaced ( its in commentary right
now
> > as I'm not sure on where to place it )
> >
> > Thanks again for your help.
> >
>
  --------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-----------------------------
> >                             Name: New Text Document.txt
> >    New Text Document.txt    Type: Plain Text (text/plain)
> >                         Encoding: base64




More information about the Javascript mailing list