[Javascript] Loop problem
David T. Lovering
dlovering at gazos.com
Tue Mar 18 12:20:07 CST 2003
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
-------------- 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/bf8fe48b/attachment.vcf>
More information about the Javascript
mailing list