SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detecting UK postcode

    Hi all,

    I have a form on a client's website which requires the user to enter a valid postcode. Recently some users have been adding fake postcodes ("zz123 456" etc...). I need to add some validation to make sure that the postcode is a valid UK postcode.

    Here a is a map showing UK postcodes: http://www.businesslistsuk.com/postcode_map.htm

    How would I go about validating a postcode? Surely I cant test *every single* postcode against what was entered by the user. My thinking is to just check the first 2 characters of the postcode, however, some postcodes allows the second character to be a number and not a letter.

    Im interested to here your comments. Thanks.

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's some info here, including a free API version.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can upload this to your database:

    uk-postcode-database-sql.zip

    [EDIT]

    Just noticed - this file can only be used to check the first part of the post code
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  4. #4
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the useful replies guys, I shall look into this in a more depth later.
    Out of interest, would there be a way in PHP to do this without having loads of if...elses?

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by yjones View Post
    Out of interest, would there be a way in PHP to do this without having loads of if...elses?
    You could use a regular expression to test your input against, but it would be quite messy and hard to maintain.
    As it is, I prefer 2ndmouse's API solution.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I've used the google maps api in the past, going through this search will explain some of the pros and cons google api to lookup uk postcode.

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    The us census puts out official zip code files every 8-12 years that you can download and place in your local table for your own quick verification.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think the op is talking about UK postcodes here.

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Woops

  10. #10
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    http://www.royalmail.com/marketing-s...d=paf_redirect

    That might be worth it for them, if they do enough business to justify the cost. Once set up they could check the submitted address against that database. I don't know if there are any free databases of UK address out there (or at least of all valid post codes)
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by yjones View Post
    Hi all, Recently some users have been adding fake postcodes ("zz123 456" etc...). I need to add some validation to make sure that the postcode is a valid UK postcode.
    UK postcodes are quite sophisticated though, and I have seen regexes around that do sort out which numbers are allowed with which letters combinations. (for example the letter "O" will never appear, it would be too easy to confuse it with a zero).

    It ought to be possible to at least identify that the first letter (or two depending on the town) is valid, and then go on to run the regex.

    See the wikipedia page on UK postcodes for a fuller run down if you do want to better understand them.

    ie maybe:

    a) check first letter (or two), on false, fail
    Then:
    b) run a (more expensive in cycles) regex pattern match, on false, fail
    or:
    c) cURL out to a lookup

    Taking your example "zz123 456" zz is clearly not one of the recognised prefixes, so abort.

    But even if it were "GU12 123" one of the aforementioned established regexes would then fail it, so abort.

    If this still permits enough bad postcodes to be a problem, then consider going out to the apis.

    UK postcode regex - google results

  12. #12
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,892
    Mentioned
    123 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by yjones View Post
    Hi all,

    I have a form on a client's website which requires the user to enter a valid postcode. Recently some users have been adding fake postcodes ("zz123 456" etc...). I need to add some validation to make sure that the postcode is a valid UK postcode.
    You can reduce the number of people accidentally getting their postcode wrong with a regex that allows any of the following formats:
    A1 1AA | A11 1AA | AA1 1AA | AA11 1AA | A1A 1AA | AA1A 1AA

    You can stop people entering non-existent postcodes that match the correct format by validating entries against the Postcode Address File.

    What you can't stop people doing is deliberately or mistakenly entering the wrong postcode. And it's trivially easy to get someone else's postcode to put in. Websites that strictly enforce a valid postcode often find that they have lots of entries for 10 Downing Street, the BBC and their own address. If people want to put in an incorrect postcode, there is nothing you can do to stop them. The question you have to ask is ... why are people so keen to put in a fake postcode? If their postcode was essential to the process (eg for delivery) then they clearly would want to put their own postcode in and get it right. So we can only assume that you're collecting postcodes for reasons that users see as irrelevant, unnecessary or at worst malevolent. So you have to ask yourself (i) do you really need the postcode, or is there some other information that you can ask for instead? and (ii) if you do need the postcode, can you explain to users why it's important to have it accurate?

    Quote Originally Posted by Cups View Post
    UK postcodes are quite sophisticated though, and I have seen regexes around that do sort out which numbers are allowed with which letters combinations. (for example the letter "O" will never appear, it would be too easy to confuse it with a zero).
    Not quote true – the letter 'O' can appear in the first portion of the code (and does for anyone living in or around Colchester, Oldham, Oxford, Portsmouth, Southampton and York), where there is a limited number of different permutations and so it couldn't be mistaken for a zero – but the letters in the second portion can never include any of CIKMOV. There are also rules about the trailing letter at the end of the first part, but that format only applies to a small number of districts in central London, so isn't relevant for most people.

  13. #13
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    http://www.ordnancesurvey.co.uk/oswe...de-point-open/

    I think that post code database is free to use, you should have a good read through its licensing stuff to make sure if its free, and any gotchas with the legal stuff
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  14. #14
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yjones View Post
    Hi all,

    I have a form on a client's website which requires the user to enter a valid postcode. Recently some users have been adding fake postcodes ("zz123 456" etc...). I need to add some validation to make sure that the postcode is a valid UK postcode.

    Here a is a map showing UK postcodes: http://www.businesslistsuk.com/postcode_map.htm

    How would I go about validating a postcode? Surely I cant test *every single* postcode against what was entered by the user. My thinking is to just check the first 2 characters of the postcode, however, some postcodes allows the second character to be a number and not a letter.

    Im interested to here your comments. Thanks.

    A regular expression check would be the easiest way to go about this, as this will determine whether the user input matches the standard format for a UK postcode. A regular expression check is built into Postcode Anywhere's Interactive_Find service, which in addition returns a list of the addresses contained within any valid UK postcode. You can try out the service free on our website.

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stevie D
    Not quote true – the letter 'O' can appear in the first portion of the code
    Thank you, I did not know that.


Bookmarks

Posting Permissions

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