p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Looping through a step in my form validation (http://p2p.wrox.com/showthread.php?t=44038)

Lucy June 15th, 2006 07:55 AM

Looping through a step in my form validation
Hi there

I was wondering if someone could help with the following problem...

I have some javascript that validates a form before it submits to a process page.

It goes something like this:

function processForm(form)
    var message = "Please:\n";
    var badFlag = false;

    if (form.elements['content_title'].value == "")
        message = message + "Enter Title.\n";
        badFlag = true;

    if ((!form.elements['content_image2'].value == "") && (form.elements['content_image2_align'].value == ""))
        message += "Choose whether to align image 2 to the left or right of your text.\n";
        badFlag = true;

    if(badFlag == true){
        return false;
    return true;

This all works fine BUT I have up to 8 possible fields in my form which run from content_image2 to content_image8 so I thought i'd put this section of code in a for loop and wrote the following...

    for (var i=2;i<=8;i=i++)
        if ((!form.elements['content_image' + i].value == "") && (form.elements['content_image' + i + '_align'].value == ""))
        message += "Choose whether to align image" + i + " to the left or right of your text.\n";
        badFlag = true;

BUT it crashes my browser!!

I haven't done much javascript programming so I don't know what i'm doing wrong so any help would be greatly appreciated. I guess I could do it the really long way but thought this would be a better solution!

Thanks in advance

Lucy xx

vinod_yadav1919 June 15th, 2006 08:21 AM

Hii Lucy!!

plz check your for loop for (var i=2;i<=8;i=i++)

Error--Infinite loop because you are using i=i++ ,that assign the value of i to 2 every time.

while it should be
CaseI Correct one----> for (var i=2;i<=8;i++)

CaseII Correct one---->
         for (var i=2;i<=8;i=j++)

As i think you are using caseI, so use (i++) rather than (i=i++)

Hope this will help you ;)

Cheers :)


vinod_yadav1919 June 15th, 2006 08:42 AM

Hi Lucky!!
Alternatively you can use
  for (i=2;i<=8;i=++i)

Hope this will help you

Cheers :)


woodyz June 15th, 2006 08:59 AM

Good catch.

Woody Z http://www.learntoprogramnow.com

Lucy June 16th, 2006 07:06 AM

Ah-ha! A rooky mistake!

Thanks for all your help with this - it now works perfectly - I just changed i=i++ to i++ and hey presto, jobs a good'un!

Cheers all

Lucy xx

All times are GMT -4. The time now is 11:48 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.