Post Code Validation (1 Viewer)

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
I have searched the forum for posts on the subject of UK postal code validation and found the reg expression that seems to be recommended for this.

I tried the expression but it will not accept this postal code? TS183AM


Code:
Like "(?:(?:A[BL]|B[ABDHLNRST]?|" & "C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|" & "H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|" & "N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|" & "T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)" & "\d(?:\d|[A-Z])? \d[A-Z]{2})"
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Looks like

Looks like I am sticking with
Code:
Like "[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]"
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Problem

Anyone know what's up with this ?
Like "[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]" Or " [A-Z][A-Z][0-9][0-9][A-Z][A-Z]"

My existing data is regarded 'invalid' when I add the or condition?
 

zambam

Registered User.
Local time
Today, 12:50
Joined
Mar 19, 2006
Messages
39
you actually need to create an input mask for your field and place the following in it as the input mask: >LL00 0LL (that also mathces the uk post code)

hope that helped!
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Upsizing

Input masks don't get upsized to MSSQL but check constraints[Validation Rules] do, they haven't when I have tried upsizing DB's before.
 

neileg

AWF VIP
Local time
Today, 20:50
Joined
Dec 4, 2002
Messages
5,975
Trouble is that your mask may accept TS18 3AM, but what will you do with W1A 1AA?

Validation of postcodes is almost impossible by looking at the alpha/numeric property of the characters. You can buy a disk with all current valid postcodes and use that for validation, but of course it's out of date before you even get it.
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Solved:
Code:
Like"[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]"Or  Like"[A-Z][A-Z][0-9][0-9][A-Z][A-Z]"
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Erm

neileg said:
So how about W1A 1AA?

Code:
Like"[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]"Or  Like"[A-Z][A-Z][0-9][0-9][A-Z][A-Z]"Or Like"[A-Z][0-9][A-Z][0-9][A-Z][A-Z]"
I don't allow spaces in my DB sorry
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
Ok

Code:
Like"[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]"Or  Like"[A-Z][A-Z][0-9][0-9][A-Z][A-Z]"Or Like"[A-Z][0-9][A-Z][0-9][A-Z][A-Z]"Or Like"[A-Z][0-9][A-Z][0-9][A-Z][A-Z]"

Are you sure these are valid postcodes?
 

neileg

AWF VIP
Local time
Today, 20:50
Joined
Dec 4, 2002
Messages
5,975
W1A1AA is the postcode for Radio 1! But there are lots of London codes that break the format you have validated. You can have SW1A1AA, as well
 

Charlie2

Registered User.
Local time
Today, 20:50
Joined
Mar 28, 2002
Messages
79
How then

How are we expected to validate then?It seems like there are a thousand and one formats
 

neileg

AWF VIP
Local time
Today, 20:50
Joined
Dec 4, 2002
Messages
5,975
neileg said:
Validation of postcodes is almost impossible by looking at the alpha/numeric property of the characters. You can buy a disk with all current valid postcodes and use that for validation, but of course it's out of date before you even get it.
I think that's what I said!
 

Simon_MT

Registered User.
Local time
Today, 20:50
Joined
Feb 26, 2007
Messages
2,177
I found six possible postcode formats in the UK.

E2 5BN
E1A 5BN
E12 5BN
EC2 5BN
EC12 5BN
EC2A 5BN

I used input masks to control although there are databases around that you can integrate into Access that will be more accurate.

Simon
 

Julian

Registered User.
Local time
Today, 20:50
Joined
Apr 23, 2009
Messages
20
ERROR WARNING Post Code Validation

Hi DCrake,

The mdb example you have linked to incorrectly accepts invalid postcode formats in the second half of the postcode.

e.g.
The correct Birmingham code "B15 1TL" is accepted.
The invalid format "B15 1TK" is also accepted.

The example using the Like statement is a nice succinct piece of code, but you should not declare it as "approved", as it leads to a false sense of security to others.:(
 

DCrake

Remembered
Local time
Today, 20:50
Joined
Jun 8, 2005
Messages
8,632
'Valid Formats (A = Alpha, N = Nueric)
'Format Example
'AN NAA M1 1AA
'AAN NAA CR2 6XH
'ANA NAA W1A 1HQ
'ANN NAA M60 1NW
'AANA NAA EC1A 1BB
'AANN NAA DN55 1PT

'The letters Q, V and X are not used in the first position.
'The letters I, J and Z are not used in the second position.
'The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
'The only letters to appear in the fourth position are A, B, E, H, M, N, P, R, V, W, X and Y.
'The second half of the Postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
'*GIR 0AA is a Postcode that was issued historically and does not confirm to current rules on valid Postcode formats, It is however, still in use.

The second part of the postcode only contends with invalid characters not the overall make up of the second part. None of the letters you stated as being invalid appear in the list. It may be that the actual postcode is invalid or has yet to be released by the Post Office.

You did not indicate what you determine as being invalid, a more informed respose would be of benefit.

Having scrutinised the Like statements it appears that there is an invalid mask. This code was provided by an "Approved" source. I have subsequently changed the mask accordingly.

It appeared it was allowing K charaters in the second part of the postcode.

The last two elements of each like statement should read

A-BD-HJMNP-UW-Z


Sorry for that. Will update my web site version as well.
 
Last edited:

Users who are viewing this thread

Top Bottom