imageknitting

Author Topic: UK Postcode Format  (Read 2094 times)

carbon-uk

  • Beginner
  • *
  • Posts: 29
UK Postcode Format
« on: October 26, 2011, 13:30:26 PM »
OK so Royal Mail will not accept postcodes unless there is a space before the last 3 characters.

For example:

NW11DB - Should read NW1 1DB

Any suggestions on the best way to achieve this with fool proof accuracy? I guess input validation, but I'm new to PHP...

stinga

  • Development Team
  • Sr. Member
  • *
  • Posts: 850
    • Squangle ltd
Re: UK Postcode Format
« Reply #1 on: October 27, 2011, 15:09:08 PM »
What don't they accept? I have never had anything rejected because of a missing space, I don't think there is any spec. that says there must be a space or how big.
Go to a different post office might solve the problem.... :-)
Stinga.
532621 products in 747 categories with 15749 products in 1 category.
                                             Document Complete   Fully Loaded
                Load Time First Byte Start Render   Time      Requests      Time      Requests
First View     2.470s     0.635s     1.276s          2.470s       31            2.470s      31
Repeat View  1.064s     0.561s     1.100s          1.064s       4             1.221s       4

carbon-uk

  • Beginner
  • *
  • Posts: 29
Re: UK Postcode Format
« Reply #2 on: October 27, 2011, 15:16:44 PM »
We genuinely have had a CSV file refused due to postcode format. They state that they have to have a space before the last 3 characters.

stinga

  • Development Team
  • Sr. Member
  • *
  • Posts: 850
    • Squangle ltd
Re: UK Postcode Format
« Reply #3 on: October 28, 2011, 02:40:49 AM »
In that case you are going to need to first check that the country is UK.
I would go the other way and just make it the format I want, so remove spaces and then put a space before the last 3 bytes.
That will give you what you want... until you find a post code that has 2 or 4 bytes after the space! :-)

You are obviously doing something special to need the use of a csv file.
Stinga.
532621 products in 747 categories with 15749 products in 1 category.
                                             Document Complete   Fully Loaded
                Load Time First Byte Start Render   Time      Requests      Time      Requests
First View     2.470s     0.635s     1.276s          2.470s       31            2.470s      31
Repeat View  1.064s     0.561s     1.100s          1.064s       4             1.221s       4

Hutson

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 737
  • VirtueMart Version: 2.0.20b
Re: UK Postcode Format
« Reply #4 on: September 19, 2012, 12:05:08 PM »
My site customers (mainly UK) mistyped postcodes and addresses leaving me having to clean up the mess with my couriers.

Spent ages on regex checks and decided that I needed to improve the speed of address entry.

With this, I get a PAF (Postal Address Format) formatted address and save time on money on chasing up customers and couriers.

Easy to configure and greatly improves the Customer checkout experience.  Hopefully lowering cart abandonment rates.
Had a couple of queries and responses were within 1 hour of raising the query!!

I can even test this out with no upfront cost, you do of course have to pay a tiny amount for PAF lookup going forward but to me it is a small price to pay for less abandonments!

http://www.craftyclicks.co.uk/web-service/shopping-cart-plugins

Before you ask:-
I am NOT anything to do with this company, nor am I being paid for this post!! 
I just wanted to support service providers that actually provide a great service and hopefully keep them developing for Virtuemart!
regards
Andrew

Joomla 2.5.11
Php 5.3.8

Hutson

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 737
  • VirtueMart Version: 2.0.20b
Re: UK Postcode Format
« Reply #5 on: September 24, 2012, 11:05:56 AM »
If a UK customer gets the postcode set incorrectly it is a real pain!!

To ensure it gets entered in a recognisable format (from a uk PAF perspective)  you can use regex checking.

Update your administration/components/com_virtuemart/classes/ps_userfield.php

Around line 712 you will see this code:-

Code: [Select]
$optional_check = '';
if( VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION') {
$optional_check = '&& form.register_account.checked';
}
    // We have skipped email in the first loop above!
    // Now let's handle email address validation
    if( isset( $required_fields['email'] )) {
   
    echo '
if( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(form.email.value))) {
alert( \''. str_replace("'","\\'",$VM_LANG->_('REGWARN_MAIL',false)) .'\');
return false;
}';

}


Just add the check for people from the UK for the postcode format:-
Code: [Select]
$optional_check = '';
if( VM_REGISTRATION_TYPE == 'OPTIONAL_REGISTRATION') {
$optional_check = '&& form.register_account.checked';
}

/// NEW CODE GOES HERE
   
// Added uk specific regex zip code check in user address!
        if( isset( $required_fields['zip'] )) {
echo '
if( form.country.value == "GBR"  && !(/^([a-zA-Z]{1,2}[0-9rR][0-9a-zA-Z]? [0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2})$/.test(form.zip.value))) {
alert( \'Please enter a valid UK post code - e.g. NG17 8AA \');
return false;
}';
}

//quorvia end
///NEW CODE END

    // We have skipped email in the first loop above!
    // Now let's handle email address validation
   
    if( isset( $required_fields['email'] )) {




Enjoy!


regards
Andrew

Joomla 2.5.11
Php 5.3.8

VirtueMart Forum

Re: UK Postcode Format
« Reply #5 on: September 24, 2012, 11:05:56 AM »