Are patches to S2member being accepted? / UK Postcode

This is a question for @clavaque

Are patches to s2member still being accepted?

I’ve made a small number of patches to implement a Custom Registration/Profile Field for the UK postcodes. It’s implemented in exactly the same as the US Zipcode, Canadian Zipcode and US/Canadian Zip codes in only three files.

/src/includes/classes/custom-reg-fields.inc.php
/src/includes/menu-pages/menu-pages-s.js
/src/includes/s2member.js
3 Likes

Hi @clavaque
I DM’d you my little effort to get UK postcodes up and running in s2member. Do let me know know what you think.

Gerard

1 Like

Thanks, Gerard! Sure, it’s welcome.

I’ll review it to see if it needs any tweaks or if it makes sense in the release.

:slight_smile:

1 Like

Here you go.

s2member-v260317.0418.zip (1.4 MB)

I reviewed it and made a few adjustments so it fits cleanly with the existing custom field handling. The UK postcode support is now wired through the same areas as the existing zipcode formats:

  • server-side validation
  • admin-side expected-value selection
  • front-end validation
  • matching minified JS for the release package

I didn’t keep the separate postcode-formatting helper as submitted, but the validation-side support is in place and the package now looks consistent for testing.

Please take a look when you have a moment and let me know if you spot anything I should fix before release.

:slight_smile:

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 :sweat_smile: 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 :scream:

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 :sweat_smile:

Also, thanks for checking this so quickly :robot:

And yes, UK Postcodes are a nightmare :sweat_smile:

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 :+1: Thanx :sweat_smile:

Of course! I had already written it in the draft. It is very appreciated and welcome. :pray:

In the text I included I mention …

Got it on the format notes. I’ll review this and probably make some adjustments before the release. I’ll post an update here once I have it.

:slight_smile:

Please try this one:

s2member-v260318.0404.zip (1.4 MB)

:slight_smile:

1 Like

Looks good :sweat_smile::+1:

I’ve just DM you some notes, compiled from my previous responses here.

Thank you! :slight_smile:

1 Like

:slight_smile:

1 Like

Whoop!

:ok_hand::laughing:

1 Like