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 9 of 9
  1. #1
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts

    Validating all textareas in a form

    I have a code that validates all "type" inputs and some Id's that are excluded for this validation also. the problem i ran into is that i have about 5 textarea's.

    I need to somehow include these in the validation, how would i attempt this. Thank you for any help

    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 != "radio" && f.elements[i].name != "other_text") 
    
      {
            alert ( "Please fill in the necassary box.");
      f.elements[i].style.backgroundColor="#ff2000";
      valid = false;
      break;
      }
     }
        return valid;
    }

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I added an extra for loop in which should check the textarea's to see if they have a value once all the 'type = text' fields have been validated. Though I'm not positive that it works.
    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 != "other_text") 
    
      {
            alert ( "Please fill in the necassary box.");
      f.elements[i].style.backgroundColor="#ff2000";
      valid = false;
      break;
      }
     }
     var ta = f.getElementsByTagName('textarea');
     for(i=0; i<ta.length; i++){
     {
         if(ta[i].value==''){
              alert ( "Please fill in the necassary box.");
      f.ta[i].style.backgroundColor="#ff2000";
      valid = false;
      break;
         }
      }
    
        return valid;
    }
    Last edited by ninnypants; 08-06-2008 at 05:50 PM. Reason: code fix

  • #3
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts
    Its giving me an "object expected" error. Im not sure why but its killing this function and saying "checkIt() is not defined"

    Code:
    function checkIt()
    {
    if(document.erod.agreed.checked == true) document.erod.Submit.disabled = false
    else document.erod.Submit.disabled = true
    }

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I had:
    Code:
    f.elements[i].style.backgroundColor="#ff2000";
    When I probably should have had
    Code:
    ta[i].style.backgroundColor="#ff2000";
    I doubt that would cause a problem but these things do behave strangely at times

  • #5
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts
    ok, well i fixed the problem, we were missing a tiny little "}" at the end

    once i fixed it, it still didnt validate the textareas. not sure why this is complicated, i figured it would be a little simpler than this.

  • #6
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I've used something similar to this while working with paypal and it worked there maybe it will work here too.
    Code:
    var textArea = ['taid1','taid2','taid3','taid4'];
    for(i=0; i<textArea.length;i++){
         ta = document.getElementById(textArea[i]);
         if(!ta.value){
               alert ( "Please fill in the necassary box.");
      ta.style.backgroundColor="#ff2000";
      valid = false;
      break;
    }

  • Users who have thanked ninnypants for this post:

    jcdevelopment (08-06-2008)

  • #7
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts
    sorry, i am not sure where to place that at, would i place it after the first break?

  • #8
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Same place as I put that other section after the for loop. If this doesn't work try setting the 'ta' variable to the textarea value. It should work I used something like this to pass values into a paypal comment field while keeping the comment editable. It is in action here

  • #9
    Senior Coder jcdevelopment's Avatar
    Join Date
    Oct 2007
    Location
    Cowboy Nation
    Posts
    2,179
    Thanks
    174
    Thanked 257 Times in 257 Posts
    thank you ninnypants for all of your help, i messed around and added another string to it. thank you for helping though!

    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" || String(f.elements[i].tagName).toUpperCase()=="TEXTAREA") && f.elements[i].value=="" && f.elements[i].name != "nom_title" && f.elements[i].type != "radio" && f.elements[i].name != "other_text" )
     
     
    
      {
            alert ( "Please fill in the necassary box.");
      f.elements[i].style.backgroundColor="#CC3333";
      valid = false;
      break;
      }
     }
        return valid;
    }


  •  

    Posting Permissions

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