Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts

    Form keeps submiting even though not valid

    I have this script that use to work, but now its not. It will have an alert box and say "please fill in necassary boxes". Even though i have it going false if not valid.

    Will someone take a look and let me know. Thank you.

    JS

    Code:
    function validate_form ( )
    {
        var valid = true;
        var f = document.erod; 
     for( var i=0; i < f.elements.length; ++i)
     {
      if( String(f.elements[i].type).toLowerCase()=="text" && f.elements[i].value=="" && f.elements[i].name != "nom_title" && f.elements[i].type != "checkbox" && f.elements[i].name != "other_text") 
    
      {
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";
            valid = false;
      }
      
     }
       return valid; 
    }
    HTML

    Code:
    <form name="erod" onsubmit="return validate_form ( );" action="ebbyRose.php" method="post">
    Thanks in advance!

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Is this:
    Code:
    String(f.elements[i].type).toLowerCase()=="text"
    Supposed to be
    Code:
    toString(f.elements[i].type).toLowerCase()=="text"
    Also do you really need a toString because you could also use
    Code:
    f.elements[i].getAttribute("type")=="text"
    and that should return a the attribute in string form.

  • Users who have thanked ninnypants for this post:

    jcdevelopment (08-05-2008)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Code:
    function validate_form(){
        var valid = true;
        var f = document.erod; 
        for(var i=0;i < f.elements.length;++i){
           if(String(f.elements[i].type).toLowerCase()=="text" &&
              f.elements[i].value=="" &&
              f.elements[i].name != "nom_title" &&
              f.elements[i].type != "checkbox" &&
              f.elements[i].name != "other_text"){
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";
            valid = false;
          }
       }
       return valid; 
    }
    elements[i] can be in the same time text and checkbox and same logic apply for names, nom_title and other_text

    Edit: I don't know how I see ==, instead of !=, so I was wrong
    regards
    Last edited by oesxyl; 08-05-2008 at 11:45 PM.

  • Users who have thanked oesxyl for this post:

    jcdevelopment (08-05-2008)

  • #4
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts
    thank you ninnypants, but now it wont even validate it when i hit submit. here is the updated code

    Code:
    function validate_form ( )
    {
        var valid = true;
        var f = document.erod; 
     for( var i=0; i < f.elements.length; ++i)
     {
      if( toString(f.elements[i].type).toLowerCase()=="text" && f.elements[i].value=="" && f.elements[i].name != "nom_title" && f.elements[i].type != "radio" && f.elements[i].name != "tv_text") 
    
      {
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";
            valid = false;
      }
     }
        return valid;
    }
    EDIT** oesxyl i also tried yours and it still keeps submiting the form, im not sure whats wrong...

    here is the link if it helps any

    http://www.ebby.com/erod/ebbyRose.shtml

    Its a test so it only goes to me in email!
    Last edited by jcdevelopment; 08-05-2008 at 11:47 PM.

  • #5
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts
    ok, i figured out the problem.. its something i dont even need

    Code:
    function validate_form ( )
    {
        var valid = true;
        var f = document.erod; 
     for( var i=0; i < f.elements.length; ++i)
     {
      if( String(f.elements[i].type).toLowerCase()=="text" && f.elements[i].value=="" && f.elements[i].name != "nom_title" && f.elements[i].type != "checkbox" && f.elements[i].name != "other_text") 
    
      {
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";//reading this and submitting for some reason
            valid = false;
      }
      
     }
       return valid; 
    }
    Thanks for the help though!

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    Code:
    function validate_form(){
        var valid = true;
        var f = document.erod; 
        for(var i=0;i < f.elements.length;++i){
           if(f.elements[i].type=="text" &&
              f.elements[i].value=="" &&
              f.elements[i].name != "nom_title" &&
              f.elements[i].name != "other_text"){
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";
            valid = false;
          }
       }
       return valid; 
    }
    regards

  • #7
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,182
    Thanks
    174
    Thanked 257 Times in 257 Posts
    Quote Originally Posted by oesxyl View Post
    try this:
    Code:
    function validate_form(){
        var valid = true;
        var f = document.erod; 
        for(var i=0;i < f.elements.length;++i){
           if(f.elements[i].type=="text" &&
              f.elements[i].value=="" &&
              f.elements[i].name != "nom_title" &&
              f.elements[i].name != "other_text"){
            alert ( "Please fill in the necassary box." );
            f.elements[i].style.color="#ff20000";
            valid = false;
          }
       }
       return valid; 
    }
    regards

    This was my fault, you were right, but if you look at the hex color

    Code:
    f.elements[i].style.color="#ff20000";
    it has too many "0"s

    Code:
    f.elements[i].style.color="#ff2000";

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    I read your previous post only after my replay,
    I understand that you don't need that line, so the problem is solved,

    best regards


  •  

    Posting Permissions

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