The following snippet sits inside a function that validates various textbox fields, which are validated onsubmit. I have other calculations in other functions and I want to stop the form form submitting if those other calculations don't exist. The "total" is calculated on-the-fly, and if no items are selected, then the total box should be empty.
Everything else is working perfectly - it's only this part giving me trouble. How do I stop it from submitting the form if no total has been calculated? I've tried different variations on this code:
If that doesn't work, try posting more code and I'll test it out.
That definitely is NOT going to work since that statement is assigning the function to be run and not actually running it.
The original code should work correctly provided that the if statement actually satisfies the condition. You might try placing an alert in each part of the if statement just to make sure that the return false there is actually being called. You will probably find that the if(document.getElementById("total").value=="") is not matching all the conditions you expect it to (such as if there is a space in the field).
Thanks Amphidamas and Stephen, I've pasted the fnValid function, calculation function, and the calls. I tried the change to the fnValid call, also tried putting it in quotes, and tried putting () after it.
Also forgot to mention it is an external JavaScript file, if that makes a difference?
Code:
function fnValid() //validates user details and that radio button is selected
{
if(sName.value=="" || sName.value==" " || sName.value.length<3)
{
alert("You MUST enter your surname properly");
sName.focus();
return false;
}
else
{
var osName=("Surname: " + sName.value +"<br />");
}
if(fName.value=="" || fName.value==" " || fName.value.length<3)
{
alert("You MUST enter your name properly");
fName.focus();
return false;
}
else
{
var ofName=("First name: " +fName.value +"<br />");
}
if(addr.value=="" || addr.value==" " || addr.value.length<3)
{
alert("You MUST enter a valid address");
addr.focus();
return false;
}
else
{
var oaddr=("Address: " +addr.value +"<br />");
}
if(cit.value=="" || cit.value==" " || cit.value.length<3)
{
alert("You MUST enter a valid city");
cit.focus();
return false;
}
else
{
var ocit=("City: " +cit.value +"<br />");
}
if(pCode.value=="" || pCode.value==" " || pCode.value.length<3)
{
alert("You MUST enter a valid city");
pCode.focus();
return false;
}
else
{
var opCode=("Post Code: " +pCode.value +"<br />");
}
if(countryName.value=="" || countryName.value==" " || countryName.value.length<3)
{
alert("You MUST enter a valid country");
countryName.focus();
return false;
}
else
{
var ocountryName=("Country: " +countryName.value +"<br />");
}
if(emailAdd.value=="" || emailAdd.value==" " || emailAdd.value.length<3 || emailAdd.value.indexOf("@")==-1 || emailAdd.value.indexOf(".")==-1)
{
alert("You MUST enter a valid email address");
emailAdd.focus();
return false;
}
else
{
var oemailAdd=("Email Address: " +emailAdd.value +"<br />");
}
if(phoneNum.value=="" || phoneNum.value==" " || phoneNum.value.length<3 )
{
alert("You MUST enter a valid phone number");
phoneNum.focus();
return false;
}
else
{
var ophoneNum=("Phone Number: " +phoneNum.value +"<br />");
}
//verify radio button selected
if(document.getElementById("mastercard").checked==true)
{
return true;
}
if(document.getElementById("bankcard").checked==true)
{
return true;
}
if(document.getElementById("visa").checked==true)
{
return true;
}
if(document.getElementById("amex").checked==true)
{
return true;
}
else
{
alert("Please select your credit card type:");
return false;
}
if(document.getElementById("total").value=="")
{
alert("No items selected!");
return false;
document.getElementById("qty1").focus();
}
else
{
return true;
}
}//end fnValid
Code:
function fnSubCalc() //calculates order amount on the fly
{
subtotal.value="";
if(document.getElementById("bigDay").checked==true)
{
subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty1").value*19.95)).toFixed(2);
if (isNaN(subtotal.value)) //detects if Stupid Users are trying to enter something oether than digits.
{
alert("Not a valid quantity. Numbers only, please.");
document.getElementById("qty1").value="";
subtotal.value="";
document.getElementById("qty1").focus();
}
}
else
{
document.getElementById("qty1").value="";
}
if(document.getElementById("visitDragons").checked==true)
{
subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty2").value*15.00)).toFixed(2);
if (isNaN(subtotal.value))
{
alert("Not a valid quantity. Numbers only, please.");
document.getElementById("qty2").value="";
subtotal.value="";
document.getElementById("qty2").focus();
}
}
else
{
document.getElementById("qty2").value="";
}
if(document.getElementById("malatharSavesTheDay").checked==true)
{
subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty3").value*15.00)).toFixed(2);
if (isNaN(subtotal.value))
{
alert("Not a valid quantity. Numbers only, please.");
document.getElementById("qty3").value="";
subtotal.value="";
document.getElementById("qty3").focus();
}
}
else
{
document.getElementById("qty3").value="";
}
if(document.getElementById("adventuresInIstanbul").checked==true)
{
subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty4").value*15.00)).toFixed(2);
if (isNaN(subtotal.value))
{
alert("Not a valid quantity. Numbers only, please.");
document.getElementById("qty4").value="";
subtotal.value="";
document.getElementById("qty4").focus();
}
}
else
{
document.getElementById("qty4").value="";
}
}//end fnSubCalc
Code:
document.getElementById("qty1").onchange=fnSubCalc; //calculating live subtotals
document.getElementById("qty2").onchange=fnSubCalc; //calculating live subtotals
document.getElementById("qty3").onchange=fnSubCalc; //calculating live subtotals
document.getElementById("qty4").onchange=fnSubCalc; //calculating live subtotals
Bookmarks