UK postcode validation

Derek

Registered User.
Local time
Today, 13:24
Joined
May 4, 2010
Messages
234
Can anyone please help me in validating the UK postcode. I have a textbox and on the after update event I want to check if the customer is UK customer or non UK customer?

I hope anyone can help me to solve this problem.

Thanks
 
When I try to open the link then it gives me an error, "microsoft access canot open this file". Can you please send me the code otherwise I will try to open it when I reach home as I think because of security setup ,I am not able to open it from my office.

Thanks a lot.
Derek
 
Again I am not able to open the file ,the same error is coming ip. Can you please send me the code 4 this.

Thanks
Derek
 
What version of Access are you using and how are you attempting to open it?

Are you saving it to your machine first?
 
Hi David

Thanks a lot for this. It works fine when I give spaces between the postcode e.g bs32 0db, but if the user write bs320db then it gives invalid postcode. Can we do something so that either the system will accept the postcode without space or give guidance to user to enter the postcode in particular format.

Thanks
Derek
 
The whole point of this is to validate the entry the user has made so if they neglect to enter a space then this will invalidate the entry. How would it know where the space should be?
 
Thanks a lot David. This is exactly what I was looking for. You are brilliant.
 
From your code it looks to me as ifthe right hand section of all post codes have a length of 3 characters. if this is correct then you could create a function:
Code:
Public Function HasSpace(xstr As String)
Dim i As Integer
Dim l As Integer
Dim x

l = Len(xstr)
For i = 1 To Len(xstr)
    If Mid(xstr, i, 1) = Chr(32) Then
        x = xstr
        HasSpace = xstr
        Exit Function
    End If
Next i

xstr = Left(xstr, l - 3) & " " & Right(xstr, 3)

HasSpace = xstr

End Function

and call it from your IsPostcodeValid function
Code:
Public Function IsPostcodeValid(ByRef Postcode As String) As Boolean
    On Error GoTo ErrIsPostcodeValid
    IsPostcodeValid = False
    'Checks the postcode is a valid length
    If Len(Postcode) < 5 Or Len(Postcode) > 8 Then
        Exit Function
    End If
    'Checks for space and adds if necessary
    HasSpace Postcode
...

or have I missed something?
 
'Valid Formats (A = Alpha, N = Numeric)
'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

As you stated the 4th character from the right should always be a space therefore if the user forgets to include this then the after update event could redress the matter and insert the space accordingly.

Good call.
 
worth looking at wikipedia which explains the dificulties of pattern matching for validating UK post codes
 
To get around the different postcodes I have a table with country and Input Mask for each Postcode format.

The UK Input Masks are:

E2 5BN >L0\ 0LL;0;
E1A 5BN >L0L\ 0LL;0;
E12 5BN >L00\ 0LL;0;
EC2 5BN >LL0\ 0LL;0;
EC12 5BN >LL00\ 0LL;0;
EC2A 5BN >LL0L\ 0LL;0;

Simon
 

Users who are viewing this thread

Back
Top Bottom