More validadtion problems!

L

Leslie Isaacs

Bob

This is obviously proving problematic: now the A is accepted in the first
position, but I think each of the following 'rules' is being ignored:

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.

Is this a simple matter to fix?

As ever, I'm extremely grateful for all your help with this: it'll be
great when it's perfect!
Les



Bob Phillips said:
Well this works for those but it is not really my code so I may break
something else.

This code as it stands allows A and any second letter except I,J,Z.

What about A#, is that valid?

Function ValidatePostCode(ByVal PostCode As String)
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[A-Z][A-Z]" And _
(Parts(0) Like "[A-Z]#" Or _
Parts(0) Like "[A-Z]#[0-9A-Z]" Or _
Parts(0) Like "[A-Z][A-Z]#" Or _
Parts(0) Like "[A-Z][A-Z]#[0-9A-Z]")) Then
ValidatePostCode = (Parts(0) Like "[BEGLMSW]#*" Or _
Parts(0) Like "A[ABCDEFGHKLMNOPQRSTUVWXY]#*" Or _
Parts(0) Like "B[ABDHLNRST]#*" Or _
Parts(0) Like "C[ABFHMORTVW]#*" Or _
Parts(0) Like "D[ADEGHLNTY]#*" Or _
Parts(0) Like "E[CHNX]#*" Or _
Parts(0) Like "F[KY]#*" Or _
Parts(0) Like "G[LU]#*" Or _
Parts(0) Like "H[ADGPRSUX]#*" Or _
Parts(0) Like "I[GPV]#*" Or _
Parts(0) Like "K[ATWY]#*" Or _
Parts(0) Like "L[ADELNSU]#*" Or _
Parts(0) Like "M[EKL]#*" Or _
Parts(0) Like "N[EGNPRW]#*" Or _
Parts(0) Like "O[LX]#*" Or _
Parts(0) Like "P[AEHLOR]#*" Or _
Parts(0) Like "R[GHM]#*" Or _
Parts(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[ADFNQRSW]#*" Or _
Parts(0) Like "W[ACDFNRSV]#*" Or _
Parts(0) Like "UB#*" Or _
Parts(0) Like "YO#*" Or _
Parts(0) Like "ZE#*" And _
(Parts(1) Like "*#[!CIKMOV][!CIKMOV]"))
End If
End Function


--
HTH

Bob

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

Leslie Isaacs said:
Bob
Any of the AANN ANN formats - .e.g. AC47 7BT should be OK, but is
rejected.
Les


Bob Phillips said:
Les,

It allows AB1 1MA, AL1 1MA but no other A* combination. What should it
allow starting with A?

--
HTH

Bob

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

Bob
Many thanks for this.
I have tested lots of postcodes with your code, and the only problem I
have been able to find so far is that it does not allow an A in the
first position (which should be valid). Again, I wish I could
understand the code sufficiently to know how to amend it for this, but
I can't get it!
Thanks for not giving up on me!
Les


Until Rick signs in, try this amendment

Function ValidatePostCode(ByVal PostCode As String)
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[A-Z][A-Z]" And _
(Parts(0) Like "[A-Z]#" Or _
Parts(0) Like "[A-Z]#[0-9A-Z]" Or _
Parts(0) Like "[A-Z][A-Z]#" Or _
Parts(0) Like "[A-Z][A-Z]#[0-9A-Z]")) Then
ValidatePostCode = (Parts(0) Like "[BEGLMSW]#*" Or _
Parts(0) Like "A[BL]#*" Or _
Parts(0) Like "B[ABDHLNRST]#*" Or _
Parts(0) Like "C[ABFHMORTVW]#*" Or _
Parts(0) Like "D[ADEGHLNTY]#*" Or _
Parts(0) Like "E[CHNX]#*" Or _
Parts(0) Like "F[KY]#*" Or _
Parts(0) Like "G[LU]#*" Or _
Parts(0) Like "H[ADGPRSUX]#*" Or _
Parts(0) Like "I[GPV]#*" Or _
Parts(0) Like "K[ATWY]#*" Or _
Parts(0) Like "L[ADELNSU]#*" Or _
Parts(0) Like "M[EKL]#*" Or _
Parts(0) Like "N[EGNPRW]#*" Or _
Parts(0) Like "O[LX]#*" Or _
Parts(0) Like "P[AEHLOR]#*" Or _
Parts(0) Like "R[GHM]#*" Or _
Parts(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[ADFNQRSW]#*" Or _
Parts(0) Like "W[ACDFNRSV]#*" Or _
Parts(0) Like "UB#*" Or _
Parts(0) Like "YO#*" Or _
Parts(0) Like "ZE#*" And _
(Parts(1) Like "*#[!CIKMOV][!CIKMOV]"))
End If
End Function


--
HTH

Bob

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

Rick

OK - my mistake - sorry!
Using the new code you gave however still seems to be allowing A3Z
3AA, etc - i.e. with a prohibited Z (and other prohibited letters) in
the 3rd position.
Does this happen at your end?

Thanks as ever
Les


in message The amended code is now working ... except that it seems to be
rejecting every postcode that I eneter (even my own!).
I wish I could understand the code a little better - then I could
try to see why this is happening!

The function appears to be working fine here. You are putting the
function in a Module that you add inside the VBA editor
(Insert/Module from its menu bar), right?

Rick
 
R

Rick Rothstein \(MVP - VB\)

We're definitely getting there!

Slowly but surely. said:
My previous mistake was simply misaligning the validation column with the
postcode column - so I had AW2=ValidatePostCode(I1), etc. Now sorted.
Great!

With your new code the 3rd position is validated OK, but the following
rules appear not to be:

a.. The letters Q, V and X are not used in the first position.
a.. The letters I, J and Z are not used in the second position.
a.. In the second half of the Postcode the letters C, I, K, M, O and V are
never used.

Okay, give this function a try instead...

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If (PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[A-HK-Y][A-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")) And _
Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]") Then
ValidatePostCode = (Parts(0) Like "[BEGLMSW]#*" Or _
Parts(0) Like "A[BL]#*" Or _
Parts(0) Like "B[ABDHLNRST]#*" Or _
Parts(0) Like "C[ABFHMORTVW]#*" Or _
Parts(0) Like "D[ADEGHLNTY]#*" Or _
Parts(0) Like "E[CHNX]#*" Or _
Parts(0) Like "F[KY]#*" Or _
Parts(0) Like "G[LU]#*" Or _
Parts(0) Like "H[ADGPRSUX]#*" Or _
Parts(0) Like "I[GPV]#*" Or _
Parts(0) Like "K[ATWY]#*" Or _
Parts(0) Like "L[ADELNSU]#*" Or _
Parts(0) Like "M[EKL]#*" Or _
Parts(0) Like "N[EGNPRW]#*" Or _
Parts(0) Like "O[LX]#*" Or _
Parts(0) Like "P[AEHLOR]#*" Or _
Parts(0) Like "R[GHM]#*" Or _
Parts(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[ADFNQRSW]#*" Or _
Parts(0) Like "W[ACDFNRSV]#*" Or _
Parts(0) Like "UB#*" Or _
Parts(0) Like "YO#*" Or _
Parts(0) Like "ZE#*")
End If
End Function


Rick
 
R

Rick Rothstein \(MVP - VB\)

The impression I got from the initial website I used as a reference (this
was some 6 months ago or so) was that the "area" for the postcodes were
established and that new houses within these areas would be just pick up the
post code for the area they were built in. Are you saying that new "areas"
are being created (perhaps existing ones being subdivided)? If so, then I
can revert the function back to a simple pattern "shape" tester, but that
would mean lots of non-existent postcodes (not physically assigned ones)
would register as TRUE. You would not be able to tell if they were genuine
postcodes that are in use or simply ones that don't violate the "general"
pattern "shape" rules. Is that what you want? If so, I'll be happy to modify
the function to do it (once we iron out the discrepancies you keep
finding<g>).

Rick
 
L

Leslie Isaacs

Hi Rick

This obviously isn't easy!
With your latest code, in the ANN NAA format, certain letters are being
incorrectly rejected in the first position - e.g.A, C, D, E and others.
Similarly, in the AANN NAA format, certain letters are being incorrectly
rejected in the first position e.g. A, F, G, H and others.
Have you got a headache: I have!!
As ever, many thanks for all of this.
Les


Rick Rothstein (MVP - VB) said:
We're definitely getting there!

Slowly but surely. said:
My previous mistake was simply misaligning the validation column with the
postcode column - so I had AW2=ValidatePostCode(I1), etc. Now sorted.
Great!

With your new code the 3rd position is validated OK, but the following
rules appear not to be:

a.. The letters Q, V and X are not used in the first position.
a.. The letters I, J and Z are not used in the second position.
a.. In the second half of the Postcode the letters C, I, K, M, O and V
are never used.

Okay, give this function a try instead...

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If (PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[A-HK-Y][A-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")) And _
Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]") Then
ValidatePostCode = (Parts(0) Like "[BEGLMSW]#*" Or _
Parts(0) Like "A[BL]#*" Or _
Parts(0) Like "B[ABDHLNRST]#*" Or _
Parts(0) Like "C[ABFHMORTVW]#*" Or _
Parts(0) Like "D[ADEGHLNTY]#*" Or _
Parts(0) Like "E[CHNX]#*" Or _
Parts(0) Like "F[KY]#*" Or _
Parts(0) Like "G[LU]#*" Or _
Parts(0) Like "H[ADGPRSUX]#*" Or _
Parts(0) Like "I[GPV]#*" Or _
Parts(0) Like "K[ATWY]#*" Or _
Parts(0) Like "L[ADELNSU]#*" Or _
Parts(0) Like "M[EKL]#*" Or _
Parts(0) Like "N[EGNPRW]#*" Or _
Parts(0) Like "O[LX]#*" Or _
Parts(0) Like "P[AEHLOR]#*" Or _
Parts(0) Like "R[GHM]#*" Or _
Parts(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[ADFNQRSW]#*" Or _
Parts(0) Like "W[ACDFNRSV]#*" Or _
Parts(0) Like "UB#*" Or _
Parts(0) Like "YO#*" Or _
Parts(0) Like "ZE#*")
End If
End Function


Rick
 
R

Rick Rothstein \(MVP - VB\)

This obviously isn't easy!

It probably is easier than I am making it. said:
With your latest code, in the ANN NAA format, certain letters are being
incorrectly rejected in the first position - e.g.A, C, D, E and others.
Similarly, in the AANN NAA format, certain letters are being incorrectly
rejected in the first position e.g. A, F, G, H and others.

Okay, what about this version?

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")) Then
ValidatePostCode = (Parts(0) Like "A[0-9BL]#*" Or _
Parts(0) Like "B[0-9ABDHLNRST]#*" Or _
Parts(0) Like "C[0-9ABFHMORTVW]#*" Or _
Parts(0) Like "D[0-9ADEGHLNTY]#*" Or _
Parts(0) Like "E[0-9CHNX]#*" Or _
Parts(0) Like "F[0-9KY]#*" Or _
Parts(0) Like "G[0-9LU]#*" Or _
Parts(0) Like "H[0-9ADGPRSUX]#*" Or _
Parts(0) Like "I[0-9GPV]#*" Or _
Parts(0) Like "K[0-9ATWY]#*" Or _
Parts(0) Like "J##" Or _
Parts(0) Like "L[0-9ADELNSU]#*" Or _
Parts(0) Like "M[0-9EKL]#*" Or _
Parts(0) Like "N[0-9EGNPRW]#*" Or _
Parts(0) Like "O[0-9LX]#*" Or _
Parts(0) Like "P[0-9AEHLOR]#*" Or _
Parts(0) Like "R[0-9GHM]#*" Or _
Parts(0) Like "S[0-9AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[0-9ADFNQRSW]#*" Or _
Parts(0) Like "U[0-9B]#*" Or _
Parts(0) Like "W[0-9ACDFNRSV]#*" Or _
Parts(0) Like "Y[0-9O]#*" Or _
Parts(0) Like "Z[0-9E]#*")
End If
End Function


Rick
 
L

Les Isaacs

Rick

I hugely admire - and am extremely grateful for - your perseverence with
this!
With your latest code, A44 4BT and C44 4BT etc. are correctly accepted, but
AH44 4BT and FH44 4BT etc. are wrongly rejected: but CH44 4BT is correctly
accepted!
Over to you!
As ever ...
Les


Rick Rothstein (MVP - VB) said:
This obviously isn't easy!

It probably is easier than I am making it. said:
With your latest code, in the ANN NAA format, certain letters are being
incorrectly rejected in the first position - e.g.A, C, D, E and others.
Similarly, in the AANN NAA format, certain letters are being incorrectly
rejected in the first position e.g. A, F, G, H and others.

Okay, what about this version?

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")) Then
ValidatePostCode = (Parts(0) Like "A[0-9BL]#*" Or _
Parts(0) Like "B[0-9ABDHLNRST]#*" Or _
Parts(0) Like "C[0-9ABFHMORTVW]#*" Or _
Parts(0) Like "D[0-9ADEGHLNTY]#*" Or _
Parts(0) Like "E[0-9CHNX]#*" Or _
Parts(0) Like "F[0-9KY]#*" Or _
Parts(0) Like "G[0-9LU]#*" Or _
Parts(0) Like "H[0-9ADGPRSUX]#*" Or _
Parts(0) Like "I[0-9GPV]#*" Or _
Parts(0) Like "K[0-9ATWY]#*" Or _
Parts(0) Like "J##" Or _
Parts(0) Like "L[0-9ADELNSU]#*" Or _
Parts(0) Like "M[0-9EKL]#*" Or _
Parts(0) Like "N[0-9EGNPRW]#*" Or _
Parts(0) Like "O[0-9LX]#*" Or _
Parts(0) Like "P[0-9AEHLOR]#*" Or _
Parts(0) Like "R[0-9GHM]#*" Or _
Parts(0) Like "S[0-9AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[0-9ADFNQRSW]#*" Or _
Parts(0) Like "U[0-9B]#*" Or _
Parts(0) Like "W[0-9ACDFNRSV]#*" Or _
Parts(0) Like "Y[0-9O]#*" Or _
Parts(0) Like "Z[0-9E]#*")
End If
End Function


Rick
 
R

Rick Rothstein \(MVP - VB\)

I hugely admire - and am extremely grateful for - your perseverence
with this!

I have obsessive/compulsive tendencies... I don't really have a choice. said:
With your latest code, A44 4BT and C44 4BT etc. are correctly accepted,
but AH44 4BT and FH44 4BT etc. are wrongly rejected: but CH44 4BT is
correctly accepted!

Okay, here is where we come into the "is it an assigned" postcode area or
not... AH and FH are not. Here is a list, which I am pretty sure I used in
my original code (if not this exact list, then one similar to it), showing
all the actually assigned postcode areas...

http://www.answers.com/topic/list-of-postcode-areas-in-the-united-kingdom

Note that AB, AL are the only assigned postcodes beginning with "A" and FK,
FY are the only ones beginning with "F"; hence why my code rejects AH44 and
FH44. So, the question comes down to this... do you want to just check the
postcode "shape" without regard to whether the postcode is "real" or not; or
do you want to verify authenticity also?

Rick
 
L

Les Isaacs

Rick

You're right - that is the question - and the answer is unequivocally that I
need the rules as at
http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm.
It is immaterial where or not a valid postcode (as defined by these rules)
is actually in use yet ... because it will be sooner or later!

Hope that makes sense (I am in fact curious why anyone would want to
validate by reference to what is in current useage).

Thanks again
Les
 
L

Leslie Isaacs

Rick

I had assumed that the rules at
www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm were absolutely "necessary
and sufficient" - i.e. that any combination of alphanumerics that is in one
of the 6 given "shapes", and that meets the 5 rules about where certain
letters can and cannot appear, is valid - even if in fact such a combination
has not (yet) actually been assigned to a real-life address. Although as you
say this would allow postcodes that may technically be non-existent, I would
still consider them to be valid.

Hopefully this makes the validation code simpler than it would other wise
be, since there is no need to add the check against assigned postcodes to
the rules at the above reference.

I hope that makes sense!

Has this been the longest thread you've seen?!

Thanks as ever
Les
 
R

Rick Rothstein \(MVP - VB\)

I had assumed that the rules at
www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm were absolutely
"necessary and sufficient" - i.e. that any combination of alphanumerics
that is in one of the 6 given "shapes", and that meets the 5 rules about
where certain letters can and cannot appear, is valid - even if in fact
such a combination has not (yet) actually been assigned to a real-life
address. Although as you say this would allow postcodes that may
technically be non-existent, I would still consider them to be valid.

Hopefully this makes the validation code simpler than it would other wise
be, since there is no need to add the check against assigned postcodes to
the rules at the above reference.

Okay, give this function a try and let me know how it works out for you...

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
ValidatePostCode = (PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")))
End Function


Rick
 
L

Leslie Isaacs

Rick

* * * Eureka * * *

We've (sorry - you've) made it!!

I now feel bad for not having emphasized earlier that validation on the
basic ruleset would be sufficient, as I can see that the solution below is
much simpler than those that were aiming to include a check against assigned
postcodes.

Anyway, on behalf of all the team at PayeDoc (www.gppayroll.org.uk), a huge
Thank You for all your efforts with this (I still don't understand how it
works though!!).

Les


Rick Rothstein (MVP - VB) said:
I had assumed that the rules at
www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm were absolutely
"necessary and sufficient" - i.e. that any combination of alphanumerics
that is in one of the 6 given "shapes", and that meets the 5 rules about
where certain letters can and cannot appear, is valid - even if in fact
such a combination has not (yet) actually been assigned to a real-life
address. Although as you say this would allow postcodes that may
technically be non-existent, I would still consider them to be valid.

Hopefully this makes the validation code simpler than it would other wise
be, since there is no need to add the check against assigned postcodes to
the rules at the above reference.

Okay, give this function a try and let me know how it works out for you...

Function ValidatePostCode(ByVal PostCode As String) As Boolean
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
ValidatePostCode = (PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]" And _
(Parts(0) Like "[A-PR-UWYZ]#" Or _
Parts(0) Like "[A-PR-UWYZ]#[0-9A-HJKSTUW]" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#" Or _
Parts(0) Like "[A-PR-UWYZ][A-HK-Y]#[0-9ABEHMNPRVWXY]")))
End Function


Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top