...

View Full Version : Validating all textareas in a form



jcdevelopment
08-06-2008, 05:20 PM
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


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;
}

ninnypants
08-06-2008, 05:39 PM
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.


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;
}

jcdevelopment
08-06-2008, 05:46 PM
Its giving me an "object expected" error. Im not sure why but its killing this function and saying "checkIt() is not defined"



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

ninnypants
08-06-2008, 05:57 PM
I had:
f.elements[i].style.backgroundColor="#ff2000";
When I probably should have had

ta[i].style.backgroundColor="#ff2000";
I doubt that would cause a problem but these things do behave strangely at times

jcdevelopment
08-06-2008, 06:08 PM
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.

ninnypants
08-06-2008, 06:47 PM
I've used something similar to this while working with paypal and it worked there maybe it will work here too.


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;
}

jcdevelopment
08-06-2008, 07:20 PM
sorry, i am not sure where to place that at, would i place it after the first break?

ninnypants
08-06-2008, 07:26 PM
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 (http://www.kustomdecals.com/design.php)

jcdevelopment
08-06-2008, 07:42 PM
thank you ninnypants for all of your help, i messed around and added another string to it. thank you for helping though!



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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum