开发者

Regex to validate UK Post Codes

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 eith开发者_运维技巧er 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?


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}))$


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’.


Use this regex

^ ?(([BEGLMNSWbeglmnsw][0-9][0-9]?)|(([A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9]?)|(([ENWenw][0-9][A-HJKSTUWa-hjkstuw])|([ENWenw][A-HK-Ya-hk-y][0-9][ABEHMNPRVWXYabehmnprvwxy])))) ?[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜