tags:

views:

183

answers:

2

I have an application that uses the following Regex to validate UK post Codes.

(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})

If I understand this regex correctly Post codes allowed should be either this post code:

GIR 0AA

Or then we can compose the first part of the post code as follows:

Either

ANN (where A is any letter except for QVX and N is any number)

or

ABNN (where A is any letter except for QVX, B any letter except for IJZ and N is any number)

or

ANC (where A is any letter except for QVX, N is any number and C is any letter from A-H and then J, K, S, T, U, W)

or

ABND (where A is any letter except for QVX, B any letter except for IJZ, N is any number and D can be any of ABEHMNPRVWXY)

The second part of the post code is

NEE (where N is any number and E is any letter except for any of CIKMOV)

This is my understanding of the above regex.

What I can't understand is why it is allowing aaa1 1aa or aaa11 1aa

Any ideas?

+4  A: 

You haven't anchored it to the start and end of the string (^ and $)

^((GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2}))$
Greg
that's done the trickthanks
Yomismo
+3  A: 

In addition to the anchoring: I don't know what regex dialect you are using, but I've never met one where this syntax:

[A-Z-[QVX]]

excludes the characters QVX as you seem to want. In most regex that would be the character class allowing A-Z-[, followed by a literal close square bracket. To exclude QVX you'd have to say:

[A-PR-UWYZ]

In any case, it's not usually a good idea to ‘validate’ postcodes this thoroughly; postcodes are a wilier beast than most imagine, especially if you want to allow historical postcodes and oddities such as the BFPO postcodes (which you currently seem to be ignoring). You also don't want to have to update your code if and when the post office adds new codes.

Best to use a simple cursory check for obviously wrong/missing input; it's a Bad Thing indeed to deny a potential customer because you think their address is ‘invalid’.

bobince
Bart Kiers