Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2006, 07:55 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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){
        alert(message);
        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
 
Old June 15th, 2006, 08:21 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

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++)

or
CaseII Correct one---->
         j=1
         for (var i=2;i<=8;i=j++)
          {
            //statements
          }

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

Hope this will help you ;)


Cheers :)

vinod
 
Old June 15th, 2006, 08:42 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

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

Hope this will help you

Cheers :)

vinod
 
Old June 15th, 2006, 08:59 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good catch.


Woody Z http://www.learntoprogramnow.com
 
Old June 16th, 2006, 07:06 AM
Authorized User
 
Join Date: Jul 2003
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
We Based Database Step By step desireemm SQL Language 1 October 15th, 2005 08:15 PM
Looping Form Generation - Refering to Controls IP076 Pro PHP 1 December 10th, 2004 08:13 PM
Looping through form elements trekmp VS.NET 2002/2003 1 May 11th, 2004 11:01 AM
Looping through form fields & retriving the values tdaustin Classic ASP Basics 8 June 10th, 2003 03:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.