Reputation: 274
I'm pulling out my hair! I am working on a webstore that sells electronic gift certificates and but also allows check and paypal sales. I am trying to write a script to throw an alert and not allow a form to process IF both the sku field ends in the word "GIFT" AND the payment method is set to "check" or "paypal"
Here is the field that generates the SKU & the one where the payment method are
<td><a href="<CGIURL>/category.cgi?item=<SKU>" id="sku" name="sku"><SKU></a></td>
<td name="payment">
<h5>Using the following payment:</h5>
<p>Check/Money Order</p>
I can't even get the form to validate the sku field let alone test for both variables to be true!! Might have got in over my head on this one.
<script language="javascript">
function checkGC() {
var GC = document.getElementById('sku');
var filter = /^([a-zA-Z0-9_\.\-])+GIFT/;
if (filter.test(GC.value)) {
alert('Please provide an alternate payment method');
GC.focus;
return false;
}
}
</script>
Any suggestions or advice?
Thanks in advance
Upvotes: 0
Views: 54
Reputation: 2778
Change:
if(filter.test(GC.value))
to:
if(/GIFT$/.test(GC.value)) // ends with GIFT
simplest way to check for "ends with"...
and fix the bug:
GC.focus;
to:
GC.focus();
Upvotes: 1