Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Function that Validates a Candadian Postal Code St...
Author:
Shawn Redman
E-mail:
Click to e-mail author
Submitted:
7/14/2001
Version:
VB5
Compatibility:
VB5, VB6
Category:
Miscellaneous
Views:
7126
Function that Validates a Candadian Postal Code String. Returns True for: "A1B2C3", "A1B 2C3", and "A1B-2C3"; False is returned for anything else. IsAlpha() Function is utilized within.
Declarations:
'none
Code:
Option Explicit Public Function VerifyPostalCode(strCode As String) As Boolean 'Considers a Space/Dash: "??? ???". Const MAXLENGTH As Integer = 7 Dim Tmp As Long Dim Str As String Dim Sts As String Str = strCode Tmp = Len(Str) ' Bounds Checking. If the passed strCode is ' Empty, Too Large, or too Small, then Exit If ((Str & "") = "") Or (Tmp > MAXLENGTH) Or _ (Tmp < (MAXLENGTH - 1)) Then Exit Function ' Determine if there's a " " or "-" in the ' middle of the String If Tmp = MAXLENGTH Then Sts = Right$(Left$(str, 4), 1) If Not ((Sts = "-") Or (Sts = " ")) Then Exit Function End If ' " " or "-", found. Remove same. str = Left$(str, 3) & Right$(str, 3) End If ' Now we're left with: "LNLNLN" For Tmp = 1 To (MAXLENGTH - 1) Sts = Right$(Left$(str, Tmp), 1) Select Case (Tmp Mod 2) Case 1 'Odd If Not IsAlpha(Sts) Then Exit Function Case 0 'Even If Not IsNumeric(Sts) Then Exit Function End Select Next Tmp VerifyPostalCode = True End Function '''''''''''''''''''''''''''''''''''''''''' Function IsAlpha(Str As String) As Boolean Dim Alph(1 To 26) As String Dim L As Long Dim L1 As Long Dim S As String ' Populate Array with Letters from 'A' to 'Z' ' 65 To 90 is the Char Codes for A To Z For L = 65 To 90 Alph(L - 64) = Chr(L) Next L ' If by chance Str contains NULL this will ' weed it out without an Error If (Str & "") = "" Then Exit Function ' Capitalize Str because "A" <> "a" S = UCase$(Str) L = 1 ' This For-Loop contains another For-Loop ' that cycles through the Alph() Array, looking ' for a match. Once found, IsAlpha is set to True ' causing the outter For Loop to point to the next ' letter in the String, repating the search. For L1 = 1 To Len(S) IsAlpha = False For L = 1 To 26 IsAlpha = ((Right$(Left$(S, L1), 1)) = (Alph(L))) If IsAlpha Then Exit For Next L If Not IsAlpha Then Exit Function L = 1 Next L1 End Function
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement