We help IT Professionals succeed at work.
Get Started

How to validate javascript coupon code form.

savavoom
savavoom asked
on
4,107 Views
Last Modified: 2013-11-18
Hi,

I currently have a working coupon code system set on my site where when the set coupon code is entered into the form, the javascript validates the code and applies the discount to the shopping cart.

The current system only allows for one coupon code to be set up and used.

Can anyone see an  easy way to make more than one coupon code variable and allow for the different discounts to be set.

The shopping cart is paypal and the javascript uses the "discount_rate" to apply the discount to the shopping cart total.

I have attached my current working script and underneath that another script which allows for the mutlipul coupon code variables to be used.

I want to merge the two together somehow as I want the on screen validation that the first script provides.

There must be a way!

Thanks in advance.

<script language = "javascript">
<!--
function validate(text1,text2) {

if (text1 == text2)
 {
 window.alert("Coupon Code Accepted!        Click the Buy Now button to go to Paypal where your discount will be applied!");
 }
if (text1 !== text2)
 {
 window.alert("Sorry, The Coupon Code you entered is invalid. Please check and try again!");
 }
}

function CalculateOrder(form) {

 if (form.text1.value == "GOLD20")
 {
 form.discount_rate.value = "20";
 form.discount_rate2.value = "20";
 form.on3.value = "Coupon Entered";
 form.os3.value = "20% Discount Applied";
 }
}
//-->
</script>
<!-- End of Script - Place between the HEAD Tags -->


---------------------------------------------

This is the button code which checks the coupon code against the set variable:


<input type="text" name="text1">
&#160;&#160;&#160;&#160;&#160;&#160;
<input type="button" value="Check It" name="Submit" onclick=javascript:validate(text1.value,"GOLD20") >



--------------------------------------------

This code allows for multipul coupon codes to be set but there is no onscreen validation for this. How can I add on screen validation to it?


var coup1="GOLD10";
var coup2="GOLD15";
var coup3="GOLD20";

function CalculateOrder(form) {

 if (form.text1.value == coup1)
 {
 form.discount_rate.value = "10";
 form.discount_rate2.value = "10";
 form.on0.value = "Coupon";
 form.os0.value = "GOLD10 - Discount 10%";
 }
  if (form.text1.value == coup2)
 {
 form.discount_rate.value = "15";
 form.discount_rate2.value = "15";
 form.on0.value = "Coupon";
 form.os0.value = "GOLD15 - Discount 15%";
 }
  if (form.text1.value == coup3)
 {
 form.discount_rate.value = "20";
 form.discount_rate2.value = "20";
 form.on0.value = "Coupon";
 form.os0.value = "GOLD20 - Discount 20%";
 }
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE