user2029952
user2029952

Reputation: 65

RegEx for English and Australian Postcode

Could anyone point me in the right direction for a regular expression to validate an English postcode and Australian Postcode

English Eg: AA11 1AA Australian: 1111

My current expression to validate an English Postcode is:

$PostCodeRegex = '~^(GIR 0AA)|(TDCU 1ZZ)|(ASCN 1ZZ)|(BIQQ 1ZZ)|(BBND 1ZZ)'
.'|(FIQQ 1ZZ)|(PCRN 1ZZ)|(STHL 1ZZ)|(SIQQ 1ZZ)|(TKCA 1ZZ)'
. '|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]'
. '|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))'
. '|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}$~i';

Upvotes: 2

Views: 2775

Answers (2)

kguest
kguest

Reputation: 3844

This isn't directly answering your question as it's stated, but I'd honestly suggest using the PEAR Validate_AU and Validate_UK packages instead of utilising a regex that could possibly allow false positives through.

Upvotes: 0

Enissay
Enissay

Reputation: 4953

You could've google it a bit, I found plenty of answers myself...

I'm not sure about you UK regex above since it doesnt work for me, so I tried this one:

^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$

Source: http://stackoverflow.com/a/164992/1519058

And googling, I found this regex for australian postecodes:

^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$

Source: http://www.etl-tools.com/regular-expressions/is-australian-post-code.html

So now you just concatenate both using a "|", and it might gives you what you want:

^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$|^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$

You can try it HERE

Upvotes: 2

Related Questions