...

View Full Version : Javascript function to apply discount in form



westylakey
05-08-2007, 07:53 PM
Hi, i want a function to apply a discount to a form.
The discount will be applied when an age over 50 is entered
if age below 50 is entered no discount is applied.

So:
Age = 59, Get 10% discount on cost.
Age = 27, No Discount.

the costs are based on membership type chosen
Its All visible in the code anyway.
so please if anybody can help :D

I Have Attached The Code:

This Is What I Have So Far:
<script type="text/javascript">

function AgeDiscount()
if(document.myform.Age.Value < 50)
{

(document.myform.Cost.value - 10%);
Return False;
}
else
{
Return True;
}
}
</script>

It Is In The File Attached, Just Search The File For The Function.
any Q's Please, Please Ask :D

Cheers
Westy

Philip M
05-08-2007, 08:21 PM
<script type="text/javascript">

function AgeDiscount() {
var totalCost = document.myform.Cost.value;
if (parseInt(document.myform.Age.value >=50)) // number, not string
{
totalCost = totalCost * 0.9; // 10% reduction
document.myform.Cost.value = totalCost;
}
}
</script>

westylakey
05-09-2007, 09:14 AM
cheers for that.

but i have just realised there is 2 discounts.

if > 45 or Age <= 60
discount = 75%

&

if Age > 60
discount = 50%

and i would need it update the cost field if the membership type is changed or the age is changed?

any clue on this?
sorry about this.

Philip M
05-09-2007, 10:27 AM
"...but i have just realised there is 2 discounts." Is there?

I somehow have the idea that this is a student's homework assignment.

You should now be able to modify the script I gave you yourself to achieve what you want.

westylakey
05-09-2007, 03:18 PM
hi, yes it is homework, but i have tried several times, but it wont work.
but i wil try and modify the code u gave.

thanks
Westy

westylakey
05-09-2007, 03:45 PM
I have modified the script and put the age into a variable.
would this work?
because i cannot seem to get it working.
i enter an age over 60 but it doesnt affect the cost.

<script language="JavaScript" type="text/javascript">

function AgeDiscount() {
var totalCost = document.myform.Cost.value;
var Age = document.myform.Age.value;
if (parseInt(Age > 45 && Age <= 60)) // number, not string
{
totalCost = totalCost * 0.25; // 75% reduction
document.myform.Cost.value = totalCost;
}
if (parseInt(Age > 60))
{
totalCost = totalCost * 0.5; // 50% reduction
document.myform.Cost.value = totalCost;
}
}
</script>


Cheers
Westy

Philip M
05-09-2007, 04:21 PM
var Age = document.myform.Age.value;

Duplication of names could confuse the browser. By the way, it is never a good idea to use 'form' for the name of a form, or 'text' for the name of a textbox.

Change the variable age to something else (e.g. custAge) throughout, and use parseInt only once.

So...

<script type="text/javascript">
function AgeDiscount() {
var totalCost = document.myform.Cost.value;
var custAge = parseInt(document.myform.Age.value); // number not string
if (custAge > 45 && custAge <= 60)
{
totalCost = totalCost * 0.25; // 75% reduction
document.myform.Cost.value = totalCost;
}
if (custAge > 60)
{
totalCost = totalCost * 0.5; // 50% reduction
document.myform.Cost.value = totalCost;
}
}
</script>

westylakey
05-09-2007, 05:23 PM
Cheers For All Your Help!
I Now Have It Working.
added onchange="AgeDiscount()" to the cost and membership fields.

Thanks
Westy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum