Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 15th, 2006, 07:55 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old June 15th, 2006, 08:21 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
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
Reply With Quote
  #3 (permalink)  
Old June 15th, 2006, 08:42 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
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
Reply With Quote
  #4 (permalink)  
Old June 15th, 2006, 08:59 AM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good catch.


Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #5 (permalink)  
Old June 16th, 2006, 07:06 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:41 PM.


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