VirtueMart Forum

VirtueMart Dev/Coding Central: VM1 (old version) => Development Projects, Modifications, Hacks & Tweaks. VM1.1 => Shipping Modules => Topic started by: carbon-uk on October 26, 2011, 21:30:26 PM

Title: UK Postcode Format
Post by: carbon-uk 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...
Title: Re: UK Postcode Format
Post by: stinga 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.... :-)
Title: Re: UK Postcode Format
Post by: carbon-uk 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.
Title: Re: UK Postcode Format
Post by: stinga 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.
Title: Re: UK Postcode Format
Post by: AH 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 (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!
Title: Re: UK Postcode Format
Post by: AH 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:-

$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:-
$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!


Title: Re: UK Postcode Format
Post by: AH 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
Title: Re: UK Postcode Format
Post by: jimleeder123 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?
Title: Re: UK Postcode Format
Post by: jimleeder123 on November 14, 2014, 11:42:44 AM
Its ok I've done it.
Title: Re: UK Postcode Format
Post by: AH 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
Title: Re: UK Postcode Format
Post by: jimleeder123 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.
Title: Re: UK Postcode Format
Post by: AH 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/ (http://www.gjcwebdesign.com/)  has a relevant plugin for vm1