/    Sign up×
Community /Pin to ProfileBookmark

Checking %ages add up to 100%

Hello there! Saw a script a bit like what I wanted on Clickthru.net a few years ago and returned to find it’s not there anymore, and I was wondering if anyone could help me.

Quite simply, Im running a charity fundraising site, and I would like to let the users choose how the money is distributed.
I will have several textboxes under the headings “The Environment”, “The Developing World”, “Children’s Charities” etc, and one as “Total” I would like users to be able to enter how they believe the money should be split between the options as a percentage and have the script ensure that the total equals 100 before the form is submitted – if not it should instruct them to. Can anyone help me with this? I’ve never written javascript before.. and still just ‘oping I can get my head round the php im gonna need to save it in the database ?

Thanks in advance to anyone who can lend their services,
jonny platt

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JonathanNov 09.2003 — I am a very christian 15 year old, so if there is anything you need for the charity just let me know. (what charity?)

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;&lt;/TITLE&gt;
&lt;LINK REL="stylesheet" href="" type="text/css"&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1"&gt;
&lt;META NAME="keywords" CONTENT=""&gt;
&lt;META NAME="description" CONTENT=""&gt;
&lt;META NAME="author" CONTENT="Jonathan"&gt;
&lt;script language="javascript" type="text/javascript"&gt;
&lt;!--
function check(){

<i> </i> // Sorry for the messy code... but all you have to do is edit
<i> </i> // the "enviorment_name","formname","charities_name","developing_name",
<i> </i> // and the "total_name" in the following code...

<i> </i> if (document.formname.enviorment_name.value.indexOf('%')!=-1 || document.formname.developing_name.value.indexOf('%')!=-1 || document.formname.charities_name.value.indexOf('%')!=-1){
<i> </i> alert("Please do not enter the "%" sign.");
<i> </i> return false;
<i> </i> }

<i> </i> var enviorment = document.formname.enviorment_name.value * 1;
<i> </i> var developing = document.formname.developing_name.value * 1;
<i> </i> var charities = document.formname.charities_name.value * 1;

<i> </i> var add_up = enviorment + developing + charities;

<i> </i> if (add_up == 100){
<i> </i> alert("Everything seems to be in order...");
<i> </i> return true;
<i> </i> }

<i> </i> if (add_up!=100){
<i> </i> alert("The percentages you have entered do not add up to 100%");
<i> </i> return false;
<i> </i> }

<i> </i> }

<i> </i> function change_number(){
<i> </i> var a = document.formname.enviorment_name.value * 1;
<i> </i> var b = document.formname.developing_name.value * 1;
<i> </i> var c = document.formname.charities_name.value * 1;
<i> </i> document.formname.total_name.value = a+b+c;
<i> </i> }
<i> </i> //--&gt;
<i> </i> &lt;/script&gt;
<i> </i>&lt;/HEAD&gt;

<i> </i>&lt;BODY&gt;

<i> </i>&lt;form name="formname" onsubmit="return check();"&gt;&lt;br&gt;
<i> </i>The Environment&lt;br&gt;
<i> </i>&lt;input type="text" name="enviorment_name" onchange="change_number();"&gt;%&lt;br&gt;
<i> </i>The Developing World&lt;br&gt;
<i> </i>&lt;input type="text" name="developing_name" onchange="change_number();"&gt;%&lt;br&gt;
<i> </i>Children's Charities&lt;br&gt;
<i> </i>&lt;input type="text" name="charities_name" onchange="change_number();"&gt;%

<i> </i>&lt;p&gt;
<i> </i>&lt;input type="text" name="total_name"&gt;
<i> </i>&lt;/p&gt;

<i> </i>&lt;p&gt;
<i> </i>&lt;input type="submit" name="submit" value="Send Percentages"&gt;
<i> </i>&lt;/p&gt;
<i> </i>&lt;/form&gt;

<i> </i>&lt;/BODY&gt;
&lt;/HTML&gt;
×

Success!

Help @namdas 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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