Click to See Complete Forum and Search --> : Checking %ages add up to 100%


namdas
11-24-2002, 11:14 AM
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

Jonathan
11-09-2003, 12:50 AM
I am a very christian 15 year old, so if there is anything you need for the charity just let me know. (what charity?)


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

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

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

var enviorment = document.formname.enviorment_name.value * 1;
var developing = document.formname.developing_name.value * 1;
var charities = document.formname.charities_name.value * 1;

var add_up = enviorment + developing + charities;

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

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

}

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

<BODY>

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

<p>
<input type="text" name="total_name">
</p>

<p>
<input type="submit" name="submit" value="Send Percentages">
</p>
</form>

</BODY>
</HTML>