View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 10,593
Default Full proof UK postcode validation

I am not sure about the whole co Peter, I just made the identified problem
work :-).

I recall an earlier post that Jamie Collins was in that tried to produce a
catch-all solution.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Peter T" <peter_t@discussions wrote in message
...
Hi Bob,

Not sure about the following

these London codes return false -ve
W1A 1HP
EC1A 1BB

this returns false +ve
A1A1 1AA

UK postcode rules -
http://www.mailsorttechnical.com/fre...dquestions.cfm
"
Format-Example Postcode
"A" indicates an alphabetic character and "N" indicates a numeric
character.
AN NAA M1 1AA
ANN NAA M60 1NW
AAN NAA CR2 6XH
AANN NAA DN55 1PT
ANA NAA W1A 1HP
AANA NAA EC1A 1BB

The postcode GIR 0AA was issued historically and does not confirm to
current
rules on valid Postcode formats - it is however still in use for Alliance
&
Leicester Girobank Plc in Bootle.

These conventions are liable to change in the future if operationally
required.
"

I tried to fault Rick's but couldn't <g, except the anomaly GIR 0AA.

Regards,
Peter T

"Bob Phillips" wrote in message
...
Sub ValidatePostCode(ByVal PostCode As String)
'
' Validate UK Post Code
'
Dim v As Variant
Dim Outer As String, Inner As String
Dim invalid As Boolean


v = Split(PostCode, " ")
If UBound(v) = 0 Then
MsgBox "Post code " & PostCode & " does not contain a blank"
Exit Sub
End If


Outer = UCase(Trim(v(0)))
Inner = UCase(Trim(v(1)))
'
' Validate inner code .....
'
invalid = False
If Len(Inner) < 3 Then
invalid = True
Else
If Left(Inner, 1) Like "[0-9]" Then
If Mid(Inner, 2, 1) Like "[A-Z]" And Mid(Inner, 3, 1) Like "[A-Z]"
Then
Else
invalid = True
End If
Else
invalid = True
End If
End If
If invalid Then
MsgBox "Inner code " & Inner & " is invalid"
Exit Sub
End If
'
' Validate Outer code .....
'
invalid = False
Select Case Len(Outer)


Case Is = 2
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" Then
Else
invalid = True
End If
Case Is = 3
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like "[0-9]")
Then
Else
invalid = True
End If
Case Is = 4
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like "[0-9]")
_
And (Mid(Outer, 3, 1) Like "[A-Z]" Or Mid(Outer, 3, 1) Like "[0-9]")
Then
Else
invalid = True
End If
Case Else
invalid = True
End Select


If invalid Then
MsgBox "Outer code " & Outer & " is invalid"
Exit Sub
End If


MsgBox "Post Code " & PostCode & " is valid"

End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my

addy)



"Ed Peters" wrote in message
ups.com...
Hi all,

I trying to do UK postcode validation.

From a previous post by 'Toppers' I've got the code below. It looks
good however it fails if a postcode of, for example, xxxx 4at.

Does anyone have a better solution?

Thanks,

Ed


Sub MyTest()
ValidatePostCode "WX2 1BA"
End Sub


Sub ValidatePostCode(Byval PostCode as String)
'
' Validate UK Post Code
'
Dim v As Variant
Dim Outer As String, Inner As String
Dim invalid As Boolean


v = Split(PostCode, " ")
If UBound(v) = 0 Then
MsgBox "Post code " & PostCode & " does not contain a blank"
Exit Sub
End If


Outer = UCase(Trim(v(0)))
Inner = UCase(Trim(v(1)))
'
' Validate inner code .....
'
invalid = False
If Len(Inner) < 3 Then
invalid = True
Else
If Left(Inner, 1) Like "[0-9]" Then
If Mid(Inner, 2, 1) Like "[A-Z]" And Mid(Inner, 3, 1) Like "[A-
Z]" Then
Else
invalid = True
End If
Else
invalid = True
End If
End If
If invalid Then
MsgBox "Inner code " & Inner & " is invalid"
Exit Sub
End If
'
' Validate Outer code .....
'
invalid = False
Select Case Len(Outer)


Case Is = 2
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]"
Then
Else
invalid = True
End If
Case Is = 3
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[0-9]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like
"[0-9]") Then
Else
invalid = True
End If
Case Is = 4
If Left(Outer, 1) Like "[A-Z]" And Right(Outer, 1) Like "[A-Z]" _
And (Mid(Outer, 2, 1) Like "[A-Z]" Or Mid(Outer, 2, 1) Like
"[0-9]") _
And (Mid(Outer, 3, 1) Like "[A-Z]" Or Mid(Outer, 3, 1) Like
"[0-9]") Then
Else
invalid = True
End If
Case Else
invalid = True
End Select


If invalid Then
MsgBox "Outer code " & Outer & " is invalid"
Exit Sub
End If


MsgBox "Post Code " & PostCode & " is valid"