UK Postcode Custom Form Attribute

Hello all,

I’m trying to get one of my form fields (‘Postcode’) to only accept the postcode format…

Has anyone ever done this successfully?

This thread was really helpful but was JUST off the mark.

I’ve been unable to find a solution that includes all UK formats which I’ll list below…

A9 9AA

A99 9AA

AA9 9AA

AA99 9AA

A9A 9AA

AA9A 9AA

Thank you!


Here is my site Read-Only: LINK
(how to share your site Read-Only link)

Hiya!

Did you find a solution to this? I’m after validation for the first part of the postcode against an existing list and this might really help :upside_down_face:

@G_Dowling , @DC1
Helpful links to you guys: One answer would be a mix of these two links

HTML input pattern Attribute (w3schools.com)

validation - RegEx for matching UK Postcodes - Stack Overflow

Afraid not, best of luck

No worries thanks for letting me know. Looks like Jeandcc above has kindly shared some very comprehensive info on Git.

This is super helpful, thank you so much!

I’m a little overwhelmed though as it’s super in-depth!

Do you know how I could simplify this to check the first two characters of the entered postcode against an existing list? I have a list of postcodes to check e.g. “RG, ST, SL, W1” and just want to qualify the postcode that was submitted matches one of the postcodes in the list.

You basically need something like this.

Add an input field, add the “pattern” attribute to it, and the value for that attribute needs to be the Regular Expression(RegEx) that you want users to match. It could be something like ^(RG|ST|SL|W1).* which requires all the input to either start with RG,ST,SL or W1.

That’s where the second link comes in, it might help you getting started with some RegEx options. You do need to have a text at the top of the input field letting your users know what format you expect them to input, since this approach doesn’t apply masking to the input (It doesn’t change what the user inputs in the box, it just checks if matches a pattern of digits/characters)

Super helpful, thanks so much! :grin: