July 14, 2020

cm-mini

Thinking Magento

Magento - UK Postcode Validation

We came across a problem recently, which was related to exporting orders to parcel force and how the UK postcode format is required to have a space between that seperates the district code and the inward code.

Many people these days assume that there is little or no reason to have a space in their postcode and seem to take a more continental approach towards this. However, this creates problems when transporting your orders to distribution firms such as ParcelForce and they WILL NOT recognise the postcode without a space. To prevent this from happening you can add the following into your name.phtml file in the customer folder of your frontend templates.

<script type="text/javascript">
var shippingForm = new VarienForm('co-shipping-form');
Validation.addAllThese(
[
['validate-zip-international', 'Please enter a valid uk zip code.E.G. GB1 6LY', function(v) {
var country_id = 'GB'
var country = $('billing:country_id');
var element = $('billing:postcode');
if (element && ('' != element.value) && (country_id == country.value)) {
if (!element.value.match(/(^[A-Za-z]{1,2}[0-9]{1,2}[A-Z]?[\s][0-9][A-Za-z]{2}$)/ )) {
return false;
}
}
return true;
}]
]
);
</script>z
Once you'ved added this to name.phtml you can now locate the postcode inputs in billing.phtml and shipping.phtml and replace the input with the following
<input type="text" title="" name="billing[postcode]" id="billing:postcode" value="" class="validate-postcode required-entry input-text" />
This will now give you nice clean postcodes for all of your UK postcodes only and mean that there will be less complications for users checking out when it comes to payment processing and order exporting to distributors.