...

View Full Version : Form keeps submiting even though not valid



jcdevelopment
08-05-2008, 10:56 PM
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



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



<form name="erod" onsubmit="return validate_form ( );" action="ebbyRose.php" method="post">


Thanks in advance!

ninnypants
08-05-2008, 11:37 PM
Is this:
String(f.elements[i].type).toLowerCase()=="text"
Supposed to be
toString(f.elements[i].type).toLowerCase()=="text"
Also do you really need a toString because you could also use


f.elements[i].getAttribute("type")=="text"

and that should return a the attribute in string form.

oesxyl
08-05-2008, 11:40 PM
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

I don't know how I see ==, instead of !=, so I was wrongregards

jcdevelopment
08-05-2008, 11:45 PM
thank you ninnypants, but now it wont even validate it when i hit submit. here is the updated 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!

jcdevelopment
08-05-2008, 11:57 PM
ok, i figured out the problem.. its something i dont even need



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!

oesxyl
08-06-2008, 12:03 AM
try this:


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

jcdevelopment
08-06-2008, 12:25 AM
try this:


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


f.elements[i].style.color="#ff20000";

it has too many "0"s


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

oesxyl
08-06-2008, 12:35 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum