Post Reply  
 
 
Thread Tools Search this Thread
27-01-2015, 13:56   #1
offmeheadpal
Registered User
 
Join Date: Apr 2013
Posts: 42
Eircode Regex

Eircode is coming this year so someone might find this useful

Code to verify an eircode:

Code:
/^(A|C|D|E|F|H|K|N|P|R|T|V|W|X|Y)([0-9])([0-9]|W)([0-9]|A|C|D|E|F|H|K|N|P|R|T|V|W|X){4}$/


Demo PHP Code

Code:
function TidyEircode($eircode)
{
	$eircode = preg_replace("/[^A-Za-z0-9 ]/", '', $eircode); //alphanumeric only 
	$eircode = strtoupper($eircode); //specs say it has to be uppercase
	return $eircode;
}


function VerifyEircode($eircode)
{
	if (preg_match("/^(A|C|D|E|F|H|K|N|P|R|T|V|W|X|Y)([0-9])([0-9]|W)([0-9]|A|C|D|E|F|H|K|N|P|R|T|V|W|X){4}$/", $eircode)) {
		return true;
	}
	else 
	{
		return false;
	}
}



$eircode = "A65F4E2";
$eircode = TidyEircode($eircode);



if(VerifyEircode($eircode))
{
	echo "Eircode Verfied";
}
else
{
	echo "Please enter a valid Eircode";
}
offmeheadpal is offline  
Advertisement
27-01-2015, 15:38   #2
L.Jenkins
Let's Do This!!!
 
L.Jenkins's Avatar
 
Join Date: May 2009
Posts: 18,057
So if my maths is right and it should be relatively simple to work out, Eircode gives rise to 2.17 Billion possible address within the 26 Counties. The code snippet is useful as well. Could nearly port it to Java or C# quickly.
L.Jenkins is offline  
27-01-2015, 16:15   #3
FSL
Registered User
 
Join Date: Feb 2006
Posts: 1,455
How do you get 2.17 Billion. It is 15*10*10*25*25*25*25 plus the exception of D6W which gives 586,328,125

From the Eircode specification
The Routing Key:

Begins with an allowed letter
Followed by two digits

The only exception is 'D6W' which is a valid Routing Key.

The Unique Identifier:

Contains four allowed characters

An allowed character is either an allowed letter or a digit.

An allowed letter is an alphabetic character, excluding:

B, G, I, J, L, M, O, Q, S, U, Z

A digit is any of 0 to 9.

Last edited by FSL; 27-01-2015 at 16:19.
FSL is offline  
27-01-2015, 16:23   #4
L.Jenkins
Let's Do This!!!
 
L.Jenkins's Avatar
 
Join Date: May 2009
Posts: 18,057
Ah, the identifiers completely eluded me. I was under the assumption that each character had 36 possibilities. Makes more sense with routing keys as you've stated FSL.
L.Jenkins is offline  
27-01-2015, 16:40   #5
FSL
Registered User
 
Join Date: Feb 2006
Posts: 1,455
Does that regex not include W as the last of digit of the three digit part for each of the first two digits not just D6?
FSL is offline  
Advertisement
27-01-2015, 16:56   #6
rekhib
Registered User
 
Join Date: Mar 2009
Posts: 11
Very handy. I think you're missing 'Y' out of the 4 character part. This is some code for the exact same task I wrote a couple of weeks back - might be of use to someone.

Code:
<?php
function eircode_filter($eircode)
{
   /*
   TRIM, UPPERCASE AND FILTER FOR UNUSED CHARACTERS
   */
   return preg_replace('/[^A-Z0-9 ]/','',strtoupper(trim($eircode)));	
}

function eircode_validate($eircode)
{
   /*
   POSITION:
      1 = A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y
      2 = 0-9
      3 = 0-9 with the exception of W for D6W
      [OPTIONAL SPACE]
      4 - 7 = 0-9 or A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y 
   */
   return preg_match('/^(A|[C-F]|H|K|N|P|R|T|[V-Y])([0-9])([0-9]|W)( )?([0-9]|A|[C-F]|H|K|N|P|R|T|[V-Y]){4}$/',$eircode);	
}

function eircode_format_storage($eircode)
{
   /* 
   STRIP OUT OPTIONAL SPACE, UPPERCASE, CONFIRM LENGTH = 7
   */
   $eircode = preg_replace('/[^A-Z0-9]/','',strtoupper($eircode));
   return strlen($eircode) == 7 ? $eircode : false;
}

function eircode_format_display($eircode)
{
   /* 
   STRIP OUT OPTIONAL SPACE, UPPERCASE, CONFIRM LENGTH = 8 (3 + [SPACE] + 4)
   */	
   $eircode = preg_replace('/[^A-Z0-9]/','',strtoupper($eircode));
   $eircode = substr($eircode,0,3).' '.substr($eircode,3);
   return strlen($eircode) == 8 ? $eircode : false;
}


$eircodes = array(
'D08   Kf49c',
'D08 f49c',
'D08f49c',
'D08F49C',
'D08F49B',
'D6WF49C',
'D6XF49C'
);

echo '<pre>';
foreach($eircodes as $eircode)
{
	echo "Testing [".$eircode."]\r\n";
	
	// FILTER USER INPUT AND TEST
	if(eircode_validate(eircode_filter($eircode)))
	{
	   echo " - Valid Eircode\r\n";
	   echo ' - Value for user: '.eircode_format_display($eircode)."\r\n";	
	   echo ' - Value for database: '.eircode_format_storage($eircode)."\r\n";
	}
	else
	{
	   echo " - Invalid Eircode\r\n";	
	}
	
	echo "\r\n";
}
echo '</pre>';
?>
rekhib is offline  
27-01-2015, 18:59   #7
offmeheadpal
Registered User
 
Join Date: Apr 2013
Posts: 42
Quote:
Originally Posted by rekhib View Post
Very handy. I think you're missing 'Y' out of the 4 character part. This is some code for the exact same task I wrote a couple of weeks back - might be of use to someone.


Ah yes I forgot the Y in the last part, ill try to edit that. Im still trying to find a bunch of codes to use for testing it out properly
offmeheadpal is offline  
27-01-2015, 19:47   #8
rekhib
Registered User
 
Join Date: Mar 2009
Posts: 11
Quote:
Originally Posted by offmeheadpal View Post
Ah yes I forgot the Y in the last part, ill try to edit that. Im still trying to find a bunch of codes to use for testing it out properly
If you do get your hands on a list of codes, would you mind posting the link here? That would be very useful.
rekhib is offline  
28-01-2015, 10:18   #9
oscarBravo
Custodiam ipsos custodes
 
oscarBravo's Avatar
 
Join Date: May 2001
Posts: 26,997
Rather than
Code:
(A|[C-F]|H|K|N|P|R|T|[V-Y])([0-9])([0-9]|W)
for the routing code, wouldn't
Code:
(((A|[C-F]|H|K|N|P|R|T|[V-Y])[0-9][0-9])|D6W)
be more accurate? The W is only ever allowed in the context of "D6W".
oscarBravo is offline  
Thanks from:
Advertisement
28-01-2015, 18:02   #10
Procasinator
Registered User
 
Join Date: Aug 2007
Posts: 1,277
As a personal preference, I would also prefer to use one character class instead of specifying OR repetitively.

I'd probably do something like this:
Code:
^(D6W|[AC-FHKNPRTV-Y][0-9]{2})([AC-FHKNPRTV-Y0-9]{4})$
Procasinator is offline  
(2) thanks from:
18-05-2016, 09:59   #11
bren_mc
Registered User
 
Join Date: Oct 2010
Posts: 374
I've just started a project where we'll need to use an Eircode validation routine. This thread was the first result I got after a google search. I've come across a few others since that use basically the same sort of regex as provided by the OP.

However having looked into it a little more, it seems there are only 139 valid routing keys so these regex expressions seem to allow a huge number of invalid routing keys. I've resorted to this javascript regex...

^(A41|A42|A45|A63|A67|A75|A81|A82|A83|A84|A85|A86|A91|A92|A94|A96|A98|C15|D01|D02|D03|D04|D05|D06|D07|D08|D09|D10|D11|D12|D13|D14|D15|D16|D17|D18|D20|D22|D24|D6W|E21|E25|E32|E34|E41|E45|E53|E91|F12|F23|F26|F28|F31|F35|F42|F45|F52|F56|F91|F92|F93|F94|H12|H14|H16|H18|H23|H53|H54|H62|H65|H71|H91|K32|K34|K36|K45|K56|K67|K78|N37|N39|N41|N91|P12|P14|P17|P24|P25|P31|P32|P36|P43|P47|P51|P56|P61|P67|P72|P75|P81|P85|R14|R21|R32|R35|R42|R45|R51|R56|R93|R95|T12|T23|T34|T45|T56|V14|V15|V23|V31|V35|V42|V92|V93|V94|V95|W12|W23|W34|W91|X35|X42|X91|Y14|Y21|Y25|Y34|Y35){1}[(A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
bren_mc is offline  
22-05-2016, 19:24   #12
bpmurray
Registered User
 
bpmurray's Avatar
 
Join Date: Apr 2002
Posts: 1,266
Or. perhaps more compact:
Code:
^(A(4[125])|(6[37])|(75)|(8[1-6])|(9[12468]))|(C15)|(D(0[1-9])|(1[0-8])|(2[024)|(6W))|(E(2[15])|(3[24])|(4[15])|(53)|(91))|(F(12)|(2[368])|(3[15])|(4[25])|(5[26])|(9[1-4]))|(H(1[2468])|(23)|(5[34])|(6[25])|([79]1))|(K(3[246])|(45)|(56)|(67)|(78))|(N(3[79])|([49]1))|(P(1[247])|(2[45])|(3[126])|(4[37])|(5[16])|(6[17])|(7[25])|(8[15]))|(R(14)|(21)|([34][25])|(5[16])|(9[35]))|(T(12)|(23)|(34)|(45)|(56))|(V(1[45])|(23)|(3[15])|(42)|(9[2-5]))|(W(12)|(23)|(34)|(91))|(X(35)|(42)|(91))|(Y(14)|(2[15])|(3[45]))[ ]*[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
bpmurray is offline  
Thanks from:
25-05-2016, 12:28   #13
MyPeopleDrankTheSoup
Registered User
 
Join Date: Feb 2004
Posts: 2,422
i hope all your implementations allow eircode to be left blank in the form! google wallet requires a valid eircode in the postcode field when adding a credit card now
MyPeopleDrankTheSoup is offline  
25-05-2016, 13:12   #14
NinetyForNone
Registered User
 
NinetyForNone's Avatar
 
Join Date: May 2014
Posts: 272
Quote:
Originally Posted by georgiecasey View Post
google wallet requires a valid eircode in the postcode field when adding a credit card now
Had a look at my wallet and when adding card doesn't even have field for postcode
NinetyForNone is offline  
25-05-2016, 13:44   #15
MyPeopleDrankTheSoup
Registered User
 
Join Date: Feb 2004
Posts: 2,422
Quote:
Originally Posted by NinetyForNone View Post
Had a look at my wallet and when adding card doesn't even have field for postcode
sorry, i meant google wallet merchant.
MyPeopleDrankTheSoup is offline  
Thanks from:
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet