Author Topic: UK Postcode Format  (Read 24416 times)

carbon-uk

  • Beginner
  • *
  • Posts: 29
UK Postcode Format
« on: October 26, 2011, 21: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

  • Contributing Developer
  • Full Member
  • *
  • Posts: 872
    • Squangle ltd
Re: UK Postcode Format
« Reply #1 on: October 27, 2011, 23: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.
614869 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, 23: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

  • Contributing Developer
  • Full Member
  • *
  • Posts: 872
    • Squangle ltd
Re: UK Postcode Format
« Reply #3 on: October 28, 2011, 10: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.
614869 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

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: UK Postcode Format
« Reply #4 on: September 19, 2012, 20: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
A

Joomla 3.8.13
php 7.1

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: UK Postcode Format
« Reply #5 on: September 24, 2012, 19:05:56 pm »
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
A

Joomla 3.8.13
php 7.1

admiss

  • Beginner
  • *
  • Posts: 35
  • VirtueMart Version: 2.0.26 d
Re: UK Postcode Format
« Reply #6 on: January 10, 2014, 16:14:52 pm »
Hello,

Virtuemart 2.0.22b
Joomla 2.5
(multilanguage: EN, HU)

I'm looking for (free) Postcode Finder - plugin, or extension , similar to Royal Mail's http://www.royalmail.com/postcode-finder
Actuaaly, I need a registration form WITH Postcode Finder.
I wonder, if it exsist, or not(?)

Thank you.

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: UK Postcode Format
« Reply #7 on: January 10, 2014, 19:54:23 pm »
Address lookup as a plugin in the UK is unlikely to be a free service as you have to pay for the PAF files in the first instance

You are looking for a "reverse" lookup, again this is PAF based, but I do not know of one for VM as customers buying stuff for themselves, tend to know their postcode.

The lookup aims to reduce the effort and errors for address input.

Maybe someone has other options, but I do not know of them for VM
regards
A

Joomla 3.8.13
php 7.1

jimleeder123

  • Jr. Member
  • **
  • Posts: 207
    • My Website
  • VirtueMart Version: 3.0.12
Re: UK Postcode Format
« Reply #8 on: November 14, 2014, 11:01:25 am »
Hi Hutson, could you tell me how to alter the code so that postcode are only in TS1, TS3, TS4 and TS5 please?
Web Developer and blogger

Joomla 3.4.8  (2.5 on some)
VM 3.0.12
PHP 5.6 (5.4 on websites with Joomla 2.5)

http://jimleeder.co.uk

jimleeder123

  • Jr. Member
  • **
  • Posts: 207
    • My Website
  • VirtueMart Version: 3.0.12
Re: UK Postcode Format
« Reply #9 on: November 14, 2014, 11:19:53 am »
I've got it validating TS1-TS5 but I want to exclude TS2. How can I do this?
Web Developer and blogger

Joomla 3.4.8  (2.5 on some)
VM 3.0.12
PHP 5.6 (5.4 on websites with Joomla 2.5)

http://jimleeder.co.uk

jimleeder123

  • Jr. Member
  • **
  • Posts: 207
    • My Website
  • VirtueMart Version: 3.0.12
Re: UK Postcode Format
« Reply #10 on: November 14, 2014, 11:42:44 am »
Its ok I've done it.
Web Developer and blogger

Joomla 3.4.8  (2.5 on some)
VM 3.0.12
PHP 5.6 (5.4 on websites with Joomla 2.5)

http://jimleeder.co.uk

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: UK Postcode Format
« Reply #11 on: November 14, 2014, 19:34:57 pm »
Well done - regex is pretty straightforward and there are lots of testing tools on the net to see if you have got it correct
regards
A

Joomla 3.8.13
php 7.1

jimleeder123

  • Jr. Member
  • **
  • Posts: 207
    • My Website
  • VirtueMart Version: 3.0.12
Re: UK Postcode Format
« Reply #12 on: January 08, 2015, 10:40:47 am »
Hi Hutson its me again. I have a similar problem but I want to only accept postcodes that start with DH2, DH3, NE9 or NE38.

I have tried doing the line after "&&" twice seperating them with "||" for or, but it doesn't seem to work. How should I do it?
Web Developer and blogger

Joomla 3.4.8  (2.5 on some)
VM 3.0.12
PHP 5.6 (5.4 on websites with Joomla 2.5)

http://jimleeder.co.uk

jimleeder123

  • Jr. Member
  • **
  • Posts: 207
    • My Website
  • VirtueMart Version: 3.0.12
Re: UK Postcode Format
« Reply #13 on: January 08, 2015, 10:58:12 am »
I've worked it out again. I used "&&" instead of "||" and it worked.

On a different subject, do you know how to force delivery rates in Virtuemart 1 based on the postcode?
Alternatively other websites I have need delivery fees decided by order totals.

Virtuemart 1 only allows for it to be decided by weight it seems.
Web Developer and blogger

Joomla 3.4.8  (2.5 on some)
VM 3.0.12
PHP 5.6 (5.4 on websites with Joomla 2.5)

http://jimleeder.co.uk

AH

  • Global Moderator
  • Sr. Member
  • *
  • Posts: 2802
  • VirtueMart Version: 3.4
Re: UK Postcode Format
« Reply #14 on: January 08, 2015, 23:02:17 pm »
You have to code the standard_shipping and some other stuff but I cannot really do this for you as mine does lots of other  bespoke stuff

You could try to see if http://www.gjcwebdesign.com/  has a relevant plugin for vm1
regards
A

Joomla 3.8.13
php 7.1