<html>
<head>
<title>Coupon test code</title>
<style>
#error{
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<form name="couponField" action="">
<span id="error"></span>
Coupon code:
<input type="coupon" id="coupon" name="coupon">
<input type="button" id="submit" value="Apply Coupon Code">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$('#submit').click(function(){
if($('#coupon').val() == 'PROMO CODE'){
$('#error').text('correct code!');
}else{
$('#error').text('wrong code!');
}
});
</script>
</body>
</html>
The above code does the following:
1. Validate if the coupon code entered is exactly equal to ‘PROMO CODE’
2. Validate if the field is empty.
What we are trying to do:
1. Keep the validation for ‘PROMO CODE’ intact.
2. Remove the validation for the empty field.
This is a part of an order form wherein we want the users to enter the promo code. But since all the users may not have the promo code we want to give the users the option to leave the field empty. The current validation is making this field a ‘required’ field.
Any suggestions are much appreciated. Thanks!
make sure you use
===instead of==in JavaScript.