While looking at some old code at work that was doing postcode validation, I thought there must be a better way to do it with a regular expression. By chance I came across this page, UK Government Data Standards Catalogue, which documents the standard for Post Codes. They also have this XML document which basically gives the regex required.
I was going to submit it to the CFLib, but discovered there was already a IsZIPUK function which ostensibly did the same thing, dating back to about 2001! However its regular expression didn’t validate all postcodes correctly. So I submitted this as an update to that function, which has now been published.
The regular expression’s a bit of a mess, I’m no regex master, so if anyone has any tips on how to simplify it, let me know.