Skip to content

Instantly share code, notes, and snippets.

@cbourdage
Last active July 9, 2022 21:49
Show Gist options
  • Save cbourdage/79ecd1a31aa344d9fc86 to your computer and use it in GitHub Desktop.
Save cbourdage/79ecd1a31aa344d9fc86 to your computer and use it in GitHub Desktop.
PO BOX js Regex validation
/**
1.1 Checkout
- Gorilla will add custom client side validation to the shipping method address field to test if the string contains a pattern of characters that would denote that the string contains the words PO Box.
- If the validation fails the user will be displayed a message stating that the shipping address cannot be a PO Box.
? P.O. Box
? PO. Box
? PO Box
? POBox
? P O Box
? P.O Box
? PO
? Postal Box
? Post Office Box
(PO Box|POBox|PO. Box|P.O. Box|P O Box|P.O Box|Postal Box|Post Office Box|PO)
\bP(ost)?[ \.]*O(ffice)?[ \.]*Box\b
*/
var poRegex = /\bP(ost|ostal)?([ \.]*O(ffice)?)?([ \.]*Box)?\b/i;
Validation.addAllThese([
['po-box-validation', 'Shipping address cannot contain a PO Box number.', function(v, el) {
if (!Validation.get('IsEmpty').test(v)) {
if (poRegex.test(v)) {
return false;
}
}
return true;
}],
['address-select', 'Shipping address cannot contain a PO Box number.', function(v, el) {
if (!Validation.get('IsEmpty').test(v)) {
if (poRegex.test(el.options[el.selectedIndex].textContent)) {
return false;
}
}
return true;
}]
]);
/**
* Gorilla added to force validation on PO box based on the change
* in radio selection for if this billing address can be used
* for shipping.
*
* @param flag
*/
Billing.prototype.setSameShippingAddress = function(flag) {
$('shipping:same_as_billing').checked = flag;
Validation.test('address-select', $('billing-address-select'));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment