In the text I included I mention
'Must be a UK Postcode (7-8 digits w/possible space).'
which almost all postcodes adhere to. Your edit
'Must be a UK postcode (5-8 characters w/ possible space).'
is fine but its no more correct than the initial 7-8 text, as if one is being 100% precise about it, submitted RegEx strings which are 4-9 characters long would be accepted.
In the official UK government Office of National Statistics website, all of the current official postcodes are between 5-7 characters long if all the spaces are removed before testing.
SELECT MIN(LENGTH(REPLACE(Postcode, ' ', ''))) AS min_length, MAX(LENGTH(REPLACE(Postcode, ' ', ''))) AS max_length FROM postcode;
min_length 5
max_length 7
The current postcode data is available to download for free.
So the RegEx I’ve implemented is:
/^(([a-zA-Z]{1,2}[0-9][a-zA-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][a-zA-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[a-zA-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/
As this takes into account historic Postcodes which people might use even thought they’re not valid postcodes any more, so I think this is a better choice as its more forgiving and will generate less support queries
It accepts postcodes of the British Islands and Overseas Territories + Military + exceptions such as:
* ASCN 1ZZ (Ascension Island)
* STHL 1ZZ (St Helena)
* TDCU 1ZZ (Tristan da Cunha)
* BIQQ 1ZZ (British Indian Ocean Territory)
* BFPO 123 / BFPO 9999
* SAN TA1 (Santa – special historical case)🎅🎄
* AI 2640 (Anguilla)
* VG 1110 (British Virgin Islands)
* MSR 1110 (Montserrat)
* KY3 1ZZ (Cayman Islands)
* GE CX (very rare exception)
* GIR 0AA (Historical UK Government benefits payment bank, Girobank)
* GY# #AA (Guernsey / Alderney style)
This is the main reason I suggest providing users a client side function to help format user input, as it will be very very very easy for people to input what they think is a valid postcode which doesn’t pass the RegEx, which I could imagine being very frustrating for people. I’d be happy to compile some simple examples for documentation on the s2member website if you think that would help.
The following is the “official” UK Government regular expression
^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
Which can be found here:
https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/488478/Bulk_Data_Transfer_-_additional_validation_valid_from_12_November_2015.pdf
But even UK government websites used a variety of RegExs 
I had considered implementing this with a menu page option to allow people to insert their own chosen RegEx, which would default to the above RegEx if none is submitted, but I’d thought that might perhaps be a bit to ambitious for my first contribution 
Also, thanks for checking this so quickly 
And yes, UK Postcodes are a nightmare 
If you just need to use modern UK postcodes, and not include these edge cases, we suggest the following JavaScript function which will format a users input as they are typing into the SW10 0AA format. This should help avoid users submitting input which fails the regex.
Simply add the following to the fields “Other Attributes: (optional)” section:
oninput="S2_formatUKPostcode(this);"
And include this somewhere in the sites head:
<script>
function S2_formatUKPostcode(input) {
let value = input.value.replace(/[^A-Za-z0-9]/g, '').toUpperCase().slice(0, 7);
if (value.length >= 5) {
let outward = value.slice(0, 4);
let inward = value.slice(4);
input.value = `${outward} ${inward}`;
} else {
input.value = value;
}
}
</script>
If you do not want to include extra JavaScript in your website use the following JavaScript fragment in your postcode field’s “Other Attributes: (optional)” section as it does not use the < or > characters which are not permitted.
oninput="let v=this.value.replace(/[^A-Za-z0-9]/g,'').toUpperCase().substring(0,7); this.value=v.length-4-1?v.substring(0,4)+' '+v.substring(4):v;"
The current official UK postcode data is available to download for free.
PS) I’d very much appreciate a credit on the changelog
Thanx 