Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:32:41+00:00 2026-06-16T05:32:41+00:00

I am getting an infinite loop when pressing submit after filling in 3 /

  • 0

I am getting an infinite loop when pressing submit after filling in 3 / 7 of the form fields. I fill in the first and last name, and the email address.

Here is the function that I suspect the infinite loop is coming from:

function runValidation()
{
    // Element Variables
    var name_first = document.forms[0].elements["name_first"];
    var name_last = document.forms[0].elements["name_last"];
    var address_email = document.forms[0].elements["address_email"];
    var address_number = document.forms[0].elements["address_number"];
    var address_postal = document.forms[0].elements["address_postal"];
    var url_link = document.forms[0].elements["other_website"];
    var user_age = document.forms[0].elements["other_age"];

    // Arrays of elements and functions
    var userVariables = new Array(name_first, name_last, address_email, address_number, address_postal, url_link, user_age);    
    var userFunctions = new Array(valName, valName, valEmail, valNum, valCode, valLink, valAge);
    var userFuncExec = new Array();

    for (i = 0; i < userVariables.length - 1; i++)
    {
        userFuncExec[i] = userFunctions[i](userVariables[i]);

        if ( userFuncExec[i] == false )
        {       
            userVariables[i].value = "";
            userVariables[i].focus();
            return false;
        }
    }

    // If the function has not returned false , then the form is valid;
    document.forms[0].submit();

}

Here is the entire .js file:

http://pastie.org/5563532

// This is the validation for the contact form of Exquisite Taste
var debugOn = true; // toggles debug alerts.

function runValidation()
{
    // Element Variables
    var name_first = document.forms[0].elements["name_first"];
    var name_last = document.forms[0].elements["name_last"];
    var address_email = document.forms[0].elements["address_email"];
    var address_number = document.forms[0].elements["address_number"];
    var address_postal = document.forms[0].elements["address_postal"];
    var url_link = document.forms[0].elements["other_website"];
    var user_age = document.forms[0].elements["other_age"];

    // Arrays of elements and functions
    var userVariables = new Array(name_first, name_last, address_email, address_number, address_postal, url_link, user_age);    
    var userFunctions = new Array(valName, valName, valEmail, valNum, valCode, valLink, valAge);
    var userFuncExec = new Array();

    for (i = 0; i < userVariables.length - 1; i++)
    {
        userFuncExec[i] = userFunctions[i](userVariables[i]);

        if ( userFuncExec[i] == false )
        {       
            userVariables[i].value = "";
            userVariables[i].focus();
            return false;
        }
    }

    // If the function has not returned false , then the form is valid;
    document.forms[0].submit();

}

function valName(nam)
{
    // This validates whatever name is passed.

    if(nam.value.length > 1)
    {
        if(debugOn)
            alert("Name is valid");
        return true;
    }else{

        alert("Please enter a name that is at least 2 characters long.");
        return false;
    }
}

function valEmail(email)
{
    // This function checks to see if the email address entered is valid.


    // Check if the email field is less than 10 characters ( 3@3.2-3 = 10 - 11 characters for the shortest email address)

    if ( email.value.length < 10 )
    {
        alert("Your email is too short to be valid.")
        return false;
    }

    // Check to see the email has at least one period and one @ symbol

    if ( email.value.indexOf(".") < 0 || email.value.indexOf("@") < 0)
    {
        alert("The format of your email is invalid. All emails require a '.' and a '@'");
        return false;
    }

    // Check if the last index of the '.' is after the '@' symbol & make sure there is only one index of '@'

    if ( email.value.lastIndexOf(".") < email.value.indexOf("@") || email.value.indexOf("@") != email.value.lastIndexOf("@") )
    {   
        alert("Your email is invalid and may have too many @ symbols or have them in the wrong place");
        return false;
    }

    // Check to see that the index of the last '.' has at least two characters after it.

    if ( email.value.lastIndexOf(".") > email.value.length-3 )
    {
        alert("Your top level domain has to be at least 2 characters");
        return false;       
    }

    // Check to see if the split array has at least 3 characters in each section except for the last (TLD).

    var email_attributes = new Array();
    var email_attributes = email.value.split("."); // tiessen-b@webmail.uwinnipeg.ca

    for ( i = 0; i < email_attributes.length - 2; i++ ) // -2 = (-1 so length = index; and -1 so the last section isn't included.)
    {
        // If one of the characters in the array value is '@' and the string length is < 3 then it isn't long enough.
        if ( email_attributes[i].indexOf("@") > -1 )
        {
            // If the length of the string - 1 (for the '@') symbol is not considered a valid symbol.
            if ( ( email_attributes[i].length - 1 ) < 3 )
            {
                alert("Your email doesn't have a long enough section");
                return false;
            }

        }

    }

    // If it got this far it is probably a valid email address.
    alert("Your email is valid!");
    return true;

}

function valNum(num)
{
    // This function validates a 10 or 12 digit phone number

    var isNum = /[0-9]/; // If the value is a number

    // Check to see if the number length is either 10 or 12
    if ( num.value.length == 10 || num.value.length == 12)
    {
        // Make sure every character is a number.
        for (i = 0; i < num.value.length; i++)
        {
            if ( !isNum.test( num.value.charAt(i) ) )
            {
                alert("You have entered an invalid number.");
                return false;
            }
        }
    }

    else if ( num.value.length == 12 )
    {
        // split all numbers into an array with a delimiter of '-'
        var numbers = num.value.split("-");

        // check if the array length is not 3 or has 4 digits in the last section
        if ( numbers.length != 3 || numbers[0].length > 3 || numbers[1].length > 3 );
        {
            alert("Your number is not formatted correctly. Make sure it is formatted like this: 204-290-9611.");
            return false;
        }

        // loop through each section of the numbers array and make sure they are all numbers

        for ( l = 0; l < numbers.length - 1; l++ )
        {
            for ( i = 0; i < numbers[l].length; i++)
            {
                if ( !isNum.test(numbers[l].charAt(i)) )
                {
                    alert("Your phone number has invalid characters");
                    return false;
                }
            }
        }
    }else{

        alert("Phone number is invalid");
        return false;
    }
        if(debugOn)
            alert("Phone number is invalid");
        return true;    
}

function valCode(code)
{
    // This function validates a postal code.

    var valid = true;
    var isChar = /[A-z]/;
    var isNum = /[0-9]/;

    // Make sure the postal code is 6 characters long.
    if( code.value.length != 6)
    {
        alert("Please enter a 6 digit/character postal code");
        return false;

    }else{

        if ( !isChar.test( code.value.charAt(0) ) )
            valid  = false;
        if ( !isNum.test( code.value.charAt(1) ) )
            valid  = false;
        if ( !isChar.test(  code.value.charAt(2) ) )
            valid  = false;
        if ( !isNum.test( code.value.charAt(3) ) )
            valid  = false;
        if ( !isChar.test( code.value.charAt(4) ) )
            valid  = false;
        if ( !isNum.test( code.value.charAt(5) ) )
            valid  = false;

        if (valid)
        {
            if(debugOn)
                alert("Postal Code is valid");
            return true;
        }else{
            alert("Your Postal Code is formatted incorrectly.");
            return false;       
        }
    }

}

function valLink(link)
{

    if(link.value.length > 0)
    {
        linkParts = link.value.split(".");
        if ( linkParts[0] == "www" || linkParts[0] == "http://www")
        {
            if( linkParts[linkParts.length-1].length < 2 || linkParts.length < 3)
            {
                alert("Invalid domain");
                focusEmpty(link);
            }else{
                return true;
            }
        }else{
            alert("invalid host");
            focusEmpty(link);
        }
    }else{
        return true;
    }
}

function valAge(age)
{
    // This function validates the users age.
    var parsedAge = parseInt(age.value, 10);

    if( age.value.length > 0 )
    {
        if(  isNaN(parsedAge) )
        {
            alert("invalid age");
            focusEmpty(age);

        }else{

            if(age.value < 1 || age.value > 125)
            {
                alert("Is that your real age? Please try again...");
                focusEmpty(age);

            }else{
                return true;
            }
        }
    }else{
        // If the user doesn't submit anything return true and validate (Age is optional).
        if(debugOn)
            alert("Age is empty but valid.");
        return true;
    }
}

Here is the code for the HTML form:

<form method="post" enctype="text/plain" action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi" name="contactForm">

                <div>
                  <p>First Name:</p>
                  <input name="name_first" type="text" />
                </div>
                <div>
                  <p>Last Name:</p>
                  <input name="name_last" type="text" />
                </div>
                <div>
                  <p>Email Address:</p>
                  <input name="address_email" type="text" />
                </div>
                <div>
                  <p>Phone Number:</p>
                  <input name="address_number" type="text" />
                </div>
                <div>
                  <p>Postal Code:</p>
                  <input name="address_postal" type="text" />
                </div>
                <div>
                  <p>Website:</p>
                  <input name="other_website" type="text" />
                </div>
                <div>
                  <p>Age:</p>
                  <input name="other_age" type="text" />
                </div>
                <div style="text-align:right; margin-right:20px;">
                  <input name="submit" type="button" value="Send" onclick="runValidation();"/>
                </div>
              </form>

Why am I getting an infinite loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:32:42+00:00Added an answer on June 16, 2026 at 5:32 am

    Use for (var i = 0;... in your loop definitions rather than for (i = 0.

    Variable scope in javascript can be tricky to understand. The answer to What is the scope of variables in JavaScript? has a great explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting an infinite loop in the URL redirect after a user either
I'm getting an infinite redirect loop after adding SSL support to my site. I'm
No matter what I try I keep getting an infinite loop with this function:
I am getting an infinite loop (in while loop) with this function. I am
I keep getting my code caught in an infinite while loop. It is nothing
My saga with x86 assembly continues, I'm getting into an infinite loop with this
I would like to know why I am getting an infinite loop here. I
I''m getting an infinite loop with the below code. When a user hits the
Is there a way to do this without getting an infinite loop? while((my $var)
I am trying to make login validation in using httpClient and getting infinite loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.