Affordable Mobile Website Idaho

IMPROVBOY Online & Print Marketing

“Where inspiration and science create brilliant solutions.”

Call Today!Web Designer Idaho

(208) 270-6445

No PO Box for Shipping in Magento 1.9 Checkout

No PO Box for Shipping in Magento 1.9 Checkout

No PO Box for Shipping in Magento 1.9 Checkout

How to use Regular Expression to validate the shipping address fields for P.O. Boxes

Some shipping carriers do not deliver packages to P.O. Boxes. Unfortunately, Magento doesn’t have an out-of-the-box way to validate the shipping address fields in checkout to automatically not allow P.O. Box addresses. Fortunately, Magento does have a built in prototype validation. So, what might appear to be quite difficult, is actually not that difficult at all.

1) Find out what files your checkout is using for the billing and shipping addresses by turning on the template paths in your system -> configuration.

2) Billing:
For me, my billing address file is in template/persistent/checkout/onepage/billing.phtml. If your billing address file is in the base folder, copy it to your own template folder.

The first thing to do is to add a class to the two address fields. For the first address line, I’m going to use: “validate-pobox”. For the second address line, I’m going to use: “validate-pobox2”. Here’s what it would look like:

Next, we go down to the bottom of the file where the javascript is located. Just after the last bracket “}”, paste the following code:

Explanation:

Validation.add('validate-pobox2','We cannot ship to your P.O. Please check the button to ship to a different address.',function(field_value) {

The above code is adding a validation to the class, “validate-pobox2” or “validate-pobox”. The error message will be, “’We cannot ship to your P.O. Please check the button to ship to a different address.” This validation will be applied to whatever the value is in the text address field.

var regex2 = /[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]/;   

The above code for var regex and var regex2 is the regular expression for different various ways that someone might input P.O. Box.

if(field_value.match(regex2))
    {
       if (document.getElementById('billing:use_for_shipping_yes').checked == true)
       {
            return false;
          }
        return true;
    }

The above code is stating that if the billing address for either the 1st address line (regex) or the 2nd address line (regex2) matches any of the P.O. Box regular expression combinations, AND if the use billing address as the shipping address is checked, then the error message will be displayed.

    else
    {
    if(!field_value.match(regex2))
       {
      return true;
      }
    }
});

The above code is stating that if either the 1st address line (regex) or the 2nd address line does not match the P.O. Box regular expression combinations, then the billing address is good and the customer can move on to the shipping addess.

In addition, I did also add a paragraph just above the Continue button to state once again that the customer cannot ship to a P.O. Box:

3) Shipping:
Shipping is not as involved as the billing.
For me, I went to template/checkout/onepage/shipping.phtml

Like billing, I need to add a class to both of the address lines. Unlike billing, it can be the same class for both lines.

Next, we go down to the bottom of the file where the javascript is located. Just after the last bracket “}”, paste the following code:

The explanation of the above code is pretty much the same as the billing, but less complex.

I did also add another paragraph just above the continue button, to once again clarify that we don’t ship to P.O. Boxes:

That should do it.

Sheri Smith

Sheri Smith

Head Programmer and Graphic Artist

Sheri is the head programmer for Improvboy, and has been working in the industry since the 1990's. Her expertise is mostly in HTML, CSS, PHP, MySQL, and javascript. If you need a website, ecommerce store, or website fixes, please don't hesitate to contact her here. She will be happy to help you out.

[et_social_follow icon_style="slide" icon_shape="rounded" icons_location="left" col_number="auto" outer_color="dark"]