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?
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?