I’m trying to modify a PO Box validation script to only validate it if “Ship to this address” is selected. The form apparently uses really easy form validation. The form section is:
<input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1" <?php if ($this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?php echo $this->__('Ship to this address') ?>" onclick="$('shipping:same_as_billing').checked = true;" class="radio" /><label for="billing:use_for_shipping_yes"><?php echo $this->__('Ship to this address') ?></label></li>
<li class="control">
<input type="radio" name="billing[use_for_shipping]" id="billing:use_for_shipping_no" value="0" <?php if (!$this->isUseBillingAddressForShipping()) {?> checked="checked"<?php }?> title="<?php echo $this->__('Ship to different address') ?>" onclick="$('shipping:same_as_billing').checked = false;" class="radio" /><label for="billing:use_for_shipping_no"><?php echo $this->__('Ship to different address') ?></label>
So far, the script is
Validation.add('validate-pobox', 'One or more items purchased cannot be shipped to a PO Box', function (v){
var pattern = /\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)/;
if( document.getElementById("billing:use_for_shipping_yes").checked) // this isn't working
if (!pattern.test(v)) return true; else return false;
});
I’m not all that familiar with jQuery (I’m slowly learning!), but no matter what I try, the script still validates the address field no matter which button is selected. I’ve also tried :
if( $("billing:use_for_shipping_yes").checked)
if( $("billing:use_for_shipping_yes").is("checked"))
try adding a colon to checked. And you forgot the # for the id of the checkbox http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html
based on the code I can see it should look like this