UK postcode validation and "inputmask" (1 Viewer)

Juan

Registered User.
Local time
Today, 12:49
Joined
Feb 16, 2006
Messages
32
I came across some code on http://www.mvps.org/access/ which validates the UK postcode. It was created by John Nurick. I've used it successfully in:

Private Sub Text0_AfterUpdate()
If Not rgxValidate(Me.ActiveControl.Text, "(?:(?: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})") Then
MsgBox "I don't like " & Me.ActiveControl.Text & "."
Cancel = True
End If
End Sub

It calls a function and I could post the code but it's very long and I'm not sure how to post it here in the "compact" format. At the end of the code, John gives several constant declarations so the function can also be used to validate a US phone number, Canadian postcode etc. These are given in the format:

<Public Const rgxZIP_UK =......> (for the UK postcode)

I think it would be really helpful if all of these declarations could be made at the same time, so the function can be called when appropriate, but I'm not sure where to place these constants. They can't go after <Private Sub Text0_AfterUpdate(), nor can they go within the function as these locations cause an error.

Can someone tell me where and how these constants should be declared? I've seen several questions here and on other boards about validation of UK postcodes and I'm sure that this will be very useful.

Thanks in advance.

Juan
 

Juan

Registered User.
Local time
Today, 12:49
Joined
Feb 16, 2006
Messages
32
Sorry about the double post - my laptop crashed whilst posting. Can a mod remove the other post?

I managed to get it sorted. I went to Insert>Module and then copied the Function, along with the Public Declarations into the new module. I modified the code slightly:

Sub Text0_AfterUpdate()

If Not rgxValidate(Me.ActiveControl.Text, rgxZIP_UK) Then
MsgBox "I don't like " & Me.ActiveControl.Text & "."
Cancel = True
End If
End Sub

and everything works fine now.

I hope that this will be of use to others who need validation of UK postcode entries. As I said, it can also be used for validation of other data, such as Canadian Provinces, Canadian postcodes, US States, Australian States etc.

Juan
 

Users who are viewing this thread

Top Bottom