SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only first form in loop getting recognised

    Hi guys,

    I have a page that contains two forms. One is a login form (for registered users) and the second is a signup form (for users who need to register).

    I am trying to do some client side validation, that will detect which form the user has tried to submit, and then validate the fields for that form.

    The code I have is follows (the same fields with different ids are being used for simplicity):
    Code:
    function checkform(whichForm)
    {
            var theForms = document.forms;
            
            for (i=0;i<theForms.length;i++)
            {
                thisForm = theForms[i].getAttribute("id");
                alert (thisForm);
                
                if(thisForm=="logInForm")
                {
                   var signin_email = document.getElementById('userEmailLog');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signin_email.value.length<1)
                   {
                      alert('Please enter your email');
                      return false;
                   }
                   else return true;
                }
                if(thisForm=="signUpForm")
                {
                   var signup_name     = document.getElementById('signup_name');
                   var signup_email = document.getElementById('signup_email');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signup_name.value.length<1)
                   {
                      alert('Please enter your name');
                      return false;
                   }      
                   if(signup_email.value.length<1)
                   {
                      return false;
                   }
                   else return true;       
                }
        }
        return true;
    }
    and below that, i have another function that perorms the onsubmit for the forms (taken from "DOM Scripting" by Jeremy Keith):
    Code:
    function prepareForms()
    {
        for(var i=0; i < document.forms.length; i++)
        {
            var thisform = document.forms[i];
            
            thisform.onsubmit = function()
            {
                return checkform(this);
            }
        }    
    }
    window.onload(prepareForms);
    Now, for some reason, in the checkForm function, only the first form (log in) ever gets checked, even if I try to submit the sign up form. The way I see it, is that the loop is not getting past the first form check.

    Could somebody please advise me as to what I am doing wrong.

    Cheers
    D

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use var inside the for loop not that is going to help you, but it will limit the scoope of the variable. And

    Code:
    function checkform(whichForm){
    
    var theForms = document.forms; for (i=0;i<theForms.length;i++) {
    var thisForm = theForms[i].getAttribute("id");
    } return true;
    }
    Another thing i can't see why are you looping through forms when in prepare function you pass the form (this) as argument on submit event, it will return you the reference of the form and just use it like this:

    Code:
    function checkForms(daForm) {
    
    if (daForm.id == "LoginForm") {
    The logic goes here
    } else if (daForm.id == "signUpForm") {
    The logic goes here
    }
    }
    and the prepare function stays same
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  3. #3
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the reply.
    OK, I have altered my checkForm function, and it now reads like so:
    HTML Code:
    function checkform(whichForm)
    {
                var formId = whichForm.getAttribute("id");
                alert(formId);
                
                if(formId=="logInForm")
                {
                   var signin_email = document.getElementById('userEmailLog');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signin_email.value.length<1)
                   {
                      alert('Please enter your email');
                      return false;
                   }
                   else return true;
                }
                else if(formId=="signUpForm")
                {
                   var signup_name     = document.getElementById('signup_name');
                   var signup_email = document.getElementById('signup_email');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signup_name.value.length<1)
                   {
                      alert('Please enter your name');
                      return false;
                   }      
                   if(signup_email.value.length<1)
                   {
                      return false;
                   }
                   return true;       
                }
    }
    It now alerts the correct name corresponding to the form I submit, but now none of the code between th if statements is being executed. It only alerts the name of the form, the proceeds to submit the form.

    Any ideas?

    D

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look at the second thing i proposed.
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  5. #5
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you mean like this?:
    HTML Code:
    function checkform(whichForm)
    {      
                if(whichForm.id=="logInForm")
                {
                   var signin_email = document.getElementById('userEmailLog');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signin_email.value.length<1)
                   {
                      alert('Please enter your email');
                      return false;
                   }
                   else return true;
                }
                else if(whichForm.id=="signUpForm")
                {
                   var signup_name     = document.getElementById('signup_name');
                   var signup_email = document.getElementById('signup_email');
                   
                   alert ("The form you tried to submit was called: " +thisForm);
                   
                   if(signup_name.value.length<1)
                   {
                      alert('Please enter your name');
                      return false;
                   }      
                   if(signup_email.value.length<1)
                   {
                      return false;
                   }
                   return true;       
                }
    }
    Thanks, but I tried it, but am still not getting any results?

  6. #6
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    psokarovski, I must apologise to you, I have made a very silly error.
    Where I had the following code:
    Code:
    alert ("The form you tried to submit was called: " +thisForm);
    I had not changed "thisForm" so it was throwing up the errors.

    Thanks very much for your help, it is now working very well.

    Dave

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NP man i'm glad it's working.
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  8. #8
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again for the help with the code,
    D


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •