I'm wondering if there's a best practice for validation for the Irish Eircode format. My best attempt so far, using REGEX in JavaScript, is the following based on the official spec found on page 11 here.
(Page 11 based on the page numbers in the document, or page 12 if you include the cover)
/^[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{1}[0-9]{1}[0-9,W]{1}[\ \-]?[0-9,A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{4}$/
I didn't find any Eircode related questions on here so I thought I'd open up this one and see what other people thought, and to see what better/shorter/more efficient patterns anyone could come up with.
Edit: Removed commas as per @Asunez answer.
/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/
Currently, the only means to validate an Eircode is either manually via finder.eircode.ie (limited to 15 uses per day!) or by purchasing access to either the ECAF or ECAD databases... which is overkill for a simple Yes/No validation and they are much too expensive.
Our Eircode Address Validation service allows you to capture accurate address data using Ireland's advanced postcode platform. Recognised as the most effective way of capturing Irish addresses, the Eircode has proven to reduce the amount of failed deliveries across the Republic of Ireland.
Online forms may accept the Eircode either as two separate parts with a space between the Routing Key and Unique Identifier for example A65 F4E2 or as a single seven- character string for example A65F4E2. For more information visit Using an Eircode.
All residential and business addresses have been given a unique new Eircode. Residential addresses include every address where post is delivered. Each house on a street, each flat in an apartment block, each unit in a duplex unit and each house in a rural townland has been given an Eircode.
Since @Manwal's answer doesn't exactly do what it should, here is my attempt at shortening the regex for OP:
(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
Updated version supporting the A65 B2CD postcodes - (?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
This is basically what your Regex is, with a few changes:
[]
brackets.C-F
, V-Y
). Elsewhere it's not beneficial to add ranges, as it won't make regex shorter.It is also possible to deal with D6W
exclusively with lookbehind, but this is more of an art than regex.
See Regex Demo: here
You can also invert the character class to not include given characters, and while it doesn't make the regex shorter, it's also worth noting. However, you need to make sure that other characters (like dots, commas) are not included too. I do it by adding the \W
token.
You can try it here
According to Product guide chapter 1.5.4 allowed signs are:
----------------------------------------------------------------------- | Component | Position | Allowed characters | ----------------------------------------------------------------------- | Routing Keys | 1 | A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y | ----------------------------------------------------------------------- | Routing Keys | 2 | 0-9 | ----------------------------------------------------------------------- | Routing Keys | 3 | 0-9 with the exception of W for D6W | ----------------------------------------------------------------------- | Unique Identifier | 4 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y | ----------------------------------------------------------------------- | Unique Identifier | 5 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y | ----------------------------------------------------------------------- | Unique Identifier | 6 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y | ----------------------------------------------------------------------- | Unique Identifier | 7 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y | -----------------------------------------------------------------------
Every routing key must contain letter and two digits except ONE specific situation which is D6W
code.
So codes begening with A5W
, C6W
, V0W
are invalid.
According to chapter 1.5.1 Recommendations for Storage and Presentation
Codes stored in database shouldn't be separated with space
or dash
, should be separated but only by space
and only for displaying.
Assuming, correct regex should looks like:
/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/
Regex online tester
Ericode guide
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With