Have just hammered out the script below. What it’s supposed to do so far is check to make sure the JobName on the form isn’t blank, and take the six values below and make sure they add up to 100 (they’re all part of a formula). If either don’t test out, it sends the user an error message telling them what they need to correct. In the case of the formula, it will repeat the number the user gave back, along with the expected number.
The first step (JobName) tests out fine. It’s on the second that Javascript seems to have an issue. If I leave the script as-is, it will error out, saying the user has, for example, entered 20202020200. What it seems to be doing is adding the string values together (“20″+”20″+”20″+”20″+”20″+”0″=202020200). If I attempt to convert to integers via parseint (example (var jobname=parseint(document.calculator.Polyestrene.value)), it doesn’t give me any error at all — even if I enter values that equal well over or unde 100. Simply seems to return true. Same if I try to declare all the var’s as ints.
If I’m making some incredibly newbie mistake here I’m terribly sorry. If anyone could point me in the right direction I’d really appreciate it.
function ValidateForm(form)
{
var jobname = document.calculator.JobName.value;
var polyestrene=document.calculator.Polyestrene.value;
var polychloride=document.calculator.Polychloride.value;
var pvc=document.calculator.PVC.value;
var pc=document.calculator.PC.value;
var pet=document.calculator.PET.value;
var organicmaterials=document.calculator.OrganicMaterials.value;
var totalchemicals=polyestrene+polychloride+pvc+pc+pet+organicmaterials;
if (jobname==””)
{ alert (“Please fill in a name for this job.”);
return false;
}
else if (totalchemicals!=100)
{ alert (“Types of Plastic waste currently add up to “+totalchemicals+” they must equal 100.”);
return false;
}
else {
return true;
}