PDA

View Full Version : form validation shouldn't submit


Spudhead
12-13-2002, 06:16 PM
Hi,

Having problems with the validation script below; specifically that it's submitting the form, but I can't see anything in the code that would allow it to.

The script is called via:

onSubmit="return checkfields(this)"

in the form tag.

function checkfields looks like this:


function checkfields(fObj){

if (fObj.invoice.value=="" ){
alert("Please, fill in all fields marked *")
return false;
}
if (parseInt(fObj.invoice.value)!= parseInt(fObj.invoice.value)){
alert("Invoice number is not a valid number")
return false;
}

var countQuotes=parseInt(fObj.count_quotes.value);
var countChanges=parseInt(fObj.count_changes.value);
var countPurchases=parseInt(fObj.count_purchases.value);

for(i=1;i<=countQuotes;i++){
if ((fObj.elements["quote"+i].value=="")&&(fObj.elements["quote"+i+"_cost"].value!="")){
alert("You have deleted a quote description\nwithout deleting its associated cost.\n\nIf you do not wish to include a quote on the\ninvoice, please delete both its description and cost.");
fObj.elements["quote"+i+"_cost"].focus();
return false;
}
if ((fObj.elements["quote"+i].value!="")&&(fObj.elements["quote"+i+"_cost"].value=="")){
alert("You have deleted a quote cost without\ndeleting the description.\n\nIf you do not wish to include a quote on the\ninvoice, please delete both its description and cost.");
fObj.elements["quote"+i].focus();
return false;
}
}


for(j=1;j<=countChanges;j++){
if ((fObj.elements["chg"+j].value=="")&&(fObj.elements["chg"+j+"_cost"].value!="")){
alert("You have deleted a job change description\nwithout deleting its associated cost.\n\nIf you do not wish to include a job change on the\ninvoice, please delete both its description and cost.");
fObj.elements["chg"+j+"_cost"].focus();
return false;
}
if ((fObj.elements["chg"+j].value!="")&&(fObj.elements["chg"+j+"_cost"].value=="")){
alert("You have deleted a job change cost without\ndeleting the description.\n\nIf you do not wish to include a job change on the\ninvoice, please delete both its description and cost.");
fObj.elements["chg"+j].focus();
return false;
}
}
return false;
}



Now, the problem is with the bottom of those two similar-looking loops. If I just put the first one (countQuotes) in, it works exactly as it should. The page doesn't submit because I'm still working on it - the final "return false" right at the bottom of the script will, eventually, be a "return true".

The loop that's causing the problems is the bottom one (countChanges) - which is an exact copy of the first, with the form element names changed. With that loop in, the script will, under all conditions, submit the form.

Why? How is a script that ALWAYS returns false, submitting a form that relies on it returning true?

whammy
12-13-2002, 11:44 PM
It will return true if there's a syntax error in the script.

Spudhead
12-16-2002, 11:03 AM
Ok, thanks - but I can't see any syntax error?

There's something up with it, definitely. If I make it alert(countChanges) in between those two for() loops, I can confirm that the variable is holding the correct value (in the example I'm using, it's always 1; everything behind it is database-generated.)

But, with a value of 1, then the loop:

for(j=1;j<=countChanges;j++){

should execute once. And it's not. At all. Now something as fundamental as this is bound to have a really simple answer and I'm waiting to sound stupid. But could someone point out to me why my for() loop isn't, um, looping?

whammy
12-16-2002, 11:24 PM
Is there any particular reason you're using 1? Most loops in js start with 0.

Skyzyx
12-17-2002, 02:24 AM
One thing I could suggest is to open the script with Mozilla/Netscape, then type "javascript:" into the address bar to open the JavaScript Console. If there are any errors, the console will list them.

If that doesn't work, I'd suggest posting your entire page so I can try the code myself to perhaps figure it out.

Spudhead
12-17-2002, 04:24 PM
Ugh <wipes egg from face> the reason it wouldn't work was because I'd mucked up the HTML. I'd missed an "=" from the form input tag. Doh. Cheers though :)