Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Javascript function to apply discount in form

    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

    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

    Cheers
    Westy
    Attached Files Attached Files

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    <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>
    Last edited by Philip M; 05-08-2007 at 09:05 PM.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    "...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.

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    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>
    Last edited by Philip M; 05-09-2007 at 04:46 PM. Reason: Added a note.

  • #8
    New Coder
    Join Date
    May 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank You

    Cheers For All Your Help!
    I Now Have It Working.
    added onchange="AgeDiscount()" to the cost and membership fields.

    Thanks
    Westy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •