邮政编码验证和相似的符号

时间:2016-10-31 12:21:21

标签: vba

我不熟悉“赞”符号,#和*使用过的地方,我想在这里进行一些验证。这段代码在网上,想知道它是否应该如此。代码,加上我已经包含的一些评论:

 Function ValidPostCode(ByVal PostCode As String) As Boolean
  '----------------------------------------------------------
   ' Deals with the postcodes of the form:
   'AN NAA
   'ANN NAA
   'AAN NAA
   'ANA NAA
   'AANA NAA
   'AANN NAA
   'has issues with spaces and obscure entries like "England".
    'Notes from Wiki::
 'As all formats end with 9AA, the first part of a postcode can easily be extracted by ignoring the last three characters
 'Areas with only single-digit districts: BR, FY, HA, HD, HG, HR, HS, HX,      JE, LD, SM, SR, WC, WN, ZE (although WC is always subdivided by a further letter, e.g. WC1A).
 'Areas with only double-digit districts: AB, LL, SO.
 'Areas with a district '0' (zero): BL, BS, CM, CR, FY, HA, PR, SL, SS (BS is the only area to have both a district 0 and a district 10).
'The following central London single-digit districts have been further divided by inserting a letter after the digit and before the space: EC1–EC4 (but not EC50), SW1, W1, WC1, WC2, and part of E1 (E1W), N1 (N1C and N1P), NW1 (NW1W) and SE1 (SE1P).
  'The letters QVX are not used in the first position.
'The letters IJZ are not used in the second position.
  'The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A.
   'The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with AA9A.
    'The final two letters do not use the letters CIKMOV, so as not to resemble digits or each other when hand-written.
 'Post code sectors are one of ten digits: 0 to 9 with 0 only used once 9 has been used in a post town, save for Croydon and Newport (see above).

 '-----------------------------------------------------------
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-9ABCDEFGHJKSTUW]" Or _
     Parts(0) Like "[A-Z][A-Z]#" Or Parts(0) Like "[A-Z][A-Z]#[0-9ABEHMNPRVWXY]")) Then
     ValidPostCode = ((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]#[AMNRVY]" 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]")
Else
    ValidPostCode = False
End If
End Function

如果有人可以提供帮助并且可能会彻底解释这些代码,我将非常感激。

谢谢

2 个答案:

答案 0 :(得分:1)

使用VB Script Regular Expressions

这样的东西
Function VALIDATE_PCODE(strPostCode As String) As Boolean

Dim R As RegExp

Set R = New RegExp

'   Validate AB## and AC# post codes

With R
    .MultiLine = False
    .IgnoreCase = True
    .Global = True
    .Pattern = "^AB\d{1,2} |AC\d{1} "   ' can use [0-9] here as  \d
End With

VALIDATE_PCODE = R.Test(strPostCode)

Set R = Nothing

End Function

修改

使用您提供的内容,您可以构建类似下面的模式。

Function Validate_PostCode(strPostCode As String) As Boolean

Const cstSingleDigit As String = _
    "BR,FY,HA,HD,HG,HR,HS,HX,JE,LD,SM,SR,WC,WN,ZE"
Const cstDoubleDigit As String = _
    "AB,LL,SO"

Dim arrTemp() As String
Dim strRegExPattern As String
Dim intCounter As Integer
Dim rgeRegExp As RegExp

'   Build the Reg Ex pattern

strRegExPattern = "^"

arrTemp = Split(cstSingleDigit, ",")

For intCounter = 0 To UBound(arrTemp)

    strRegExPattern = strRegExPattern & _
                        IIf(intCounter > 0, "|", "") & _
                        arrTemp(intCounter) & "\d{1} "

Next intCounter

Erase arrTemp

arrTemp = Split(cstDoubleDigit, ",")

For intCounter = 0 To UBound(arrTemp)

    strRegExPattern = strRegExPattern & "|" & _
                        arrTemp(intCounter) & "\d{2} "

Next intCounter

Set rgeRegExp = New RegExp

rgeRegExp.Global = True
rgeRegExp.IgnoreCase = True
rgeRegExp.Pattern = strRegExPattern

Validate_PostCode = rgeRegExp.Test(strPostCode)

End Function

答案 1 :(得分:1)

我通常使用此Reg Ex来验证邮政编码(模式总是可以改进,但这还没有让我失望) - 将返回邮政编码或#VALUE错误。

h3.kompetenceLinks {
  text-decoration: underline;
}
h3.kompetenceLinks:hover {
  color: red;
}