/    Sign up×
Community /Pin to ProfileBookmark

Javascript (or bad coding) returning Strings instead of Ints

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;
}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJul 01.2009 — All form fields contain strings as their values.

Try to convert the string to a number.

Since it is user input, make sure it [B]is [/B]a number before you make use of it.

var n= parseFloat(formfield.value);

if(isNaN(n)){

// handle or ignore bad data

}

// use the number
×

Success!

Help @bbureau spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...