[VBA] Validate UK Postcode with Regular Expression

I have made a little tool which validates UK Postcodes with different settings. UK Postcode Validation.jpg

Function: IsValidUKPostcode(Postcode,[CheckLevel],[CheckCase])
Examples: IsValidateUKPostcode (“EC1A 1BB”), IsValidateUKPostcode(“EC1A 1BB”, 1, 2)
Parameters:
Postcode is the string of postcode you want to be checked.
CheckLevel is optional, 0=check format, 1=include convention check, and 2=include Area Code check. The higher the level is set the more precise the postcode is checked. Default is 0. It can be extended and increase the precision of the checks potentially, i.e. validate Area Code and following digit(s) combination etc. However, it means more maintenance to do in order to keep the checks up-to-date as the number of postcodes increases in the future.
CheckCase is optional, 0=case insensitive, 1=letters are either all lower case or all upper case, and 2=upper case letters required. Default is 0.

The detailed rules are commented in the code below. To use the code, make sure to check Developer tab -> Visual Basic -> Tools -> References, Microsoft VBScript Regular Expressions 5.5 needs to be ticked.

Option Explicit

Enum eCheckLevel

‘ Check UK postcode format
CheckFormat = 0
‘ Check UK postcode conventions
CheckConvention = 1
‘ CheckFormat + check area code
CheckAreaCode = 2
‘————————–
‘ NOT IN USE at the moment
‘ Check Postcode existence
‘CheckExistence = 3
‘————————–

End Enum

Enum eCaseMatch

‘ can have mixed entry of lower case and upper case letters
CaseInsensitive = 0
‘ can have all upper case letters or lower case letters, not allow mix
CaseConsistent = 1
‘ accept upper case letters only
CaseUpper = 2

End Enum

Function IsValidUKPostcode(Postcode As String, Optional CheckLevel As eCheckLevel = 0, Optional CaseMatch As eCaseMatch = 0) As Boolean

Dim UPostcode

IsValidUKPostcode = False
Postcode = Trim(Postcode)
If Postcode = “” Then Exit Function

PostcodeValidation:

‘ Check Postcode Format
IsValidUKPostcode = UKPCFormat(Postcode)
If IsValidUKPostcode = False Then Exit Function
If CheckLevel = CheckFormat Then GoTo CaseValidation

‘ Check Postcode Convention
IsValidUKPostcode = UKPCConvention(Postcode)
If IsValidUKPostcode = False Then Exit Function
If CheckLevel = CheckConvention Then GoTo CaseValidation

‘ Check Postcode Area Code
IsValidUKPostcode = ValidAreaCode(Postcode)
If IsValidUKPostcode = False Then Exit Function
If CheckLevel = CheckAreaCode Then GoTo CaseValidation

CaseValidation:

Select Case CaseMatch

‘ don’t need CaseInsensitive check
Case CaseConsistent
If Postcode <> UCase(Postcode) And Postcode <> LCase(Postcode) Then IsValidUKPostcode = False
Case CaseUpper
If Postcode <> UCase(Postcode) Then IsValidUKPostcode = False

End Select

End Function

‘————————————————————————–
‘UK Postcode Format
‘An nAA, Ann nAA, AAn nAA, AAnn nAA, AnA nAA, AAnA NAA
‘n represents a digit and A represents a letter
‘First one or two letter(s) is the Area Code
‘————————————————————————–
Private Function UKPCFormat(Postcode As String) As Boolean

Dim UPostcode As String
Dim RE As RegExp

UKPCFormat = False
Postcode = Trim(Postcode)
If Postcode = “” Then Exit Function

‘ Make check case insensitive
UPostcode = UCase(Postcode)
Set RE = New RegExp

RE.Pattern = “^([A-Z][A-Z]?)([0-9]{1,2}|[0-9][A-Z])\s[0-9][A-Z]{2}”

UKPCFormat = RE.test(UPostcode)

Set RE = Nothing

End Function

‘————————————————————————–
‘UK Postcode Convention
‘First letter: Q, V and X are NOT used
‘Second letter: I, J and Z are NOT used
‘Third letter: A, B, C, D, E, F, G, H, J, K, S, T, U and W are used
‘Letter in second half(nAA): C, I, K, M, O and V are NOT used
‘————————————————————————–

Private Function UKPCConvention(Postcode As String) As Boolean

Dim UPostcode As String
Dim RE As RegExp

UKPCConvention = False
Postcode = Trim(Postcode)
If Postcode = “” Then Exit Function

‘ Make check case insensitive
UPostcode = UCase(Postcode)
Set RE = New RegExp

RE.Pattern = “^([^QVX][^IJZ]?)([0-9]{1,2}|[0-9][ABCDEFGHJKSTUW])\s[0-9][^CIKMOV]{2}”

UKPCConvention = RE.test(UPostcode)

Set RE = Nothing

End Function

‘————————————————————————–
‘UK Postcode Area Code
‘AB, AL, B, BA, BB, BD, BH, BL, BN, BR, BS, BT, CA, CB, CF, CH, CM, CO, CR,
‘CT, CV, CW, DA, DD, DE, DG, DH, DL, DN, DT, DY, E, EC, EH, EN, EX, FK, FY,
‘G, GL, GU, HA, HD, HG, HP, HR, HS, HU, HX, IG, IP, IV, KA, KT, KW, KY, L,
‘LA, LD, LE, LL, LN, LS, LU, M, ME, MK, ML, N, NE, NG, NN, NP, NR, NW, OL,
‘OX, PA, PE, PH, PL, PO, PR, RG, RH, RM, S, SA, SE, SG, SK, SL, SM, SN, SO,
‘SP, SR, SS, ST, SW, SY, TA, TD, TF, TN, TQ, TR, TS, TW, UB, W, WA, WC, WD,
‘WF, WN, WR, WS, WV, YO, ZE, GY, JE, IM
‘————————————————————————–

Private Function ValidAreaCode(Postcode As String) As Boolean

Dim UPostcode As String
Dim AreaCode As Variant
Dim PSArea As String

ValidAreaCode = False
Postcode = Trim(Postcode)
If Postcode = “” Then Exit Function
UPostcode = UCase(Postcode)

AreaCode = Array(“AB”, “AL”, “B”, “BA”, “BB”, “BD”, “BH”, “BL”, “BN”, _
“BR”, “BS”, “BT”, “CA”, “CB”, “CF”, “CH”, “CM”, “CO”, “CR”, “CT”, “CV”, “CW”, _
“DA”, “DD”, “DE”, “DG”, “DH”, “DL”, “DN”, “DT”, “DY”, “E”, “EC”, “EH”, “EN”, _
“EX”, “FK”, “FY”, “G”, “GL”, “GU”, “HA”, “HD”, “HG”, “HP”, “HR”, “HS”, “HU”, _
“HX”, “IG”, “IP”, “IV”, “KA”, “KT”, “KW”, “KY”, “L”, “LA”, “LD”, “LE”, “LL”, _
“LN”, “LS”, “LU”, “M”, “ME”, “MK”, “ML”, “N”, “NE”, “NG”, “NN”, “NP”, “NR”, _
“NW”, “OL”, “OX”, “PA”, “PE”, “PH”, “PL”, “PO”, “PR”, “RG”, “RH”, “RM”, “S”, _
“SA”, “SE”, “SG”, “SK”, “SL”, “SM”, “SN”, “SO”, “SP”, “SR”, “SS”, “ST”, “SW”, _
“SY”, “TA”, “TD”, “TF”, “TN”, “TQ”, “TR”, “TS”, “TW”, “UB”, “W”, “WA”, “WC”, _
“WD”, “WF”, “WN”, “WR”, “WS”, “WV”, “YO”, “ZE”, “GY”, “JE”, “IM”)

On Error GoTo Quit
PSArea = Left(UPostcode, 2)
If IsNumeric(Right(PSArea, 1)) Then
PSArea = Left(PSArea, 1)
End If

If UBound(Filter(AreaCode, PSArea)) > -1 Then ValidAreaCode = True

Quit:
End Function

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s