Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default UK Postcode format

    Hi,
    please could someone point me in the right direction:

    I have a mod. which looks at the postcode and decides if a customer is entitled to free delivery.

    My users are putting their postcodes in the format "po175nd".
    The expected (and correct) format for a postcode is "PO17 5ND".
    How do I validate the postcode field for new customers so that they enter their postcode in the correct format?

    (Please be gental with me: I have some HTML and JavaScript, but not much!).

    Many thanks,
    James.

  2. #2
    Join Date
    Jan 2004
    Posts
    66,444
    Plugin Contributions
    279

    Default Re: UK Postcode format

    If you only need upper-case, simply convert it:

    Conceptually:
    Code:
    $postcode = strtoupper($postcode);

    If you want to manually insert the space as the 5th character, you could manipulate it like this:

    Code:
    $postcode = str_replace(array(' ', '-'), '', $postcode);
    $postcode = substr($postcode, 0,4,) . ' ' . substr($postcode, 5);
    If you're trying to validate the order of letters and numbers etc, you'd need to write more advanced code...
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Jun 2008
    Posts
    187
    Plugin Contributions
    0

    Default Re: UK Postcode format

    into which file should this place?

    is it into the shipping module code?

    regards
    Andy.

  4. #4
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,903
    Plugin Contributions
    96

    Default Re: UK Postcode format

    There are actually 3 paths in ZenCart where the customer can enter a postcode:

    1) Account creation (/includes/modules/create_account.php)
    2) Address change during checkout (/includes/modules/checkout_new_address.php)
    3) Changes to the customer's address book (/includes/modules/pages/address_book_process/header.php)

    You'd want to apply the changes to each of those files.

    Here's a little function that will validate the format of a zipcode for the US, Canada, or Great Britain (it has the side-effect of upper-casing the $postcode value):

    Code:
    function postcode_validate(&$postcode, $country) {
      global $messageStack;
    
      $formats = array ( 38 => '#(^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] {0,1}\d[ABCEGHJ-NPRSTV-Z]\d$)#i',
                        222 => '#(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)#i',
                        223 => '#(^\d{5}$)|(^\d{5}-\d{4}$)#i');
    
      $error = false;
    
      if( array_key_exists( $country, $formats) ) {
        $temp = strtoupper( $postcode );
        if( preg_match( $formats[$country], $temp, $matches ) == 0 ) {
          $error = true;
      }
    
      return $error;
    
    }

  5. #5
    Join Date
    Jun 2005
    Location
    Cumbria, UK
    Posts
    10,266
    Plugin Contributions
    3

    Default Re: UK Postcode format

    This is VERY useful to ensure that when customers REGISTER, the postcode field auto-capitalises:

    http://www.zen-cart.com/index.php?ma...roducts_id=441

    It also ensures Name Case on other input data.

    When installed, people who register will then have their details Name Cased and the postcode capitalised - regardless of whether they use all lower case or all upper case when filling in the fields.
    20 years a Zencart User

  6. #6
    Join Date
    Mar 2010
    Location
    UK
    Posts
    445
    Plugin Contributions
    0

    Default Re: UK Postcode format

    Unlike other countries (like the US using five digits, for example) UK postcodes are somewhat complex.

    In general,
    - one or two letters,
    - followed by one or two digits OR a digit and a letter
    - followed by a space
    - followed by a digit and two letters.

    Examples:
    E1 1AA and E11 1AA - East London
    EH1 1AA and EH11 1AA - Edinburgh
    EC1 1AA and EC1X 1AA - London Central
    and others.

    Nice regex code in the long post above!

  7. #7
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,903
    Plugin Contributions
    96

    Default Re: UK Postcode format

    FWIW, the regex was found on www.regexlib.com.

 

 

Similar Threads

  1. UK Shipping by Postcode
    By Z3NN3D in forum Addon Shipping Modules
    Replies: 58
    Last Post: 15 Sep 2022, 04:38 PM
  2. v151 Denmark postcode format 1, min length 4, but 5 digits required
    By d0ugparker in forum General Questions
    Replies: 2
    Last Post: 1 Apr 2014, 02:45 PM
  3. UK Postcode Lookup
    By flobster in forum All Other Contributions/Addons
    Replies: 16
    Last Post: 19 Aug 2011, 12:06 AM
  4. UK Postcode Validation
    By babyandme in forum Managing Customers and Orders
    Replies: 0
    Last Post: 30 Aug 2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg