Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

Data validation - verifying format of a string

Hi,

     I'm having to program data validation for some forms.  Is there some easy VB commands to determine format of a string?  So that for instance:

Easy--
For phone numbers I can check:  '###-###-####'

Difficult--
For address I could verify: '# text'  where # here can be 14, 392, 1298 to allow for different length numbers in addresses?

I'm not sure if that last one would be worth the trouble, but the first one I imagine there's an actual VB string command for checking.

Thanks!

JP
0
gleznov
Asked:
gleznov
  • 3
  • 3
1 Solution
 
Bob LearnedCommented:
Regular expression validation:

Simple:
\d{1,3}).?\d{0,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,15}

Complex:
^(?n:(?<address1>(\d{1,5}(\ 1\/[234])?(\x20[A-Z]([a-z])+)+
)|(P\.O\.\ Box\ \d{1,5}))\s{1,2}(?i:(?<address2>(((APT|B
LDG|DEPT|FL|HNGR|LOT|PIER|RM|S(LIP|PC|T(E|OP))|TRLR|UNIT)\x20\w{1,5})|(BSMT|FRNT|LBBY|LOWR|OFC|PH|REAR|SIDE|UPPR)\.?)\s{1,2})?)(?<city>[A-Z]([a-z])+(\.?)(\x20[A-Z]([a-z])+){0,2})\,
\x20(?<state>A[LKSZRAP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADL
N]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]
|T[NX]|UT|V[AIT]|W[AIVY])\x20(?<zipcode>(?!0{5})\d{5}(-\d
{4})?))$

Bob
0
 
gleznovAuthor Commented:
whooooooooah.  Whaaaaaat?

JP
0
 
gleznovAuthor Commented:
How do I go about implementing that?

JP
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Bob LearnedCommented:
A bit overwhelming, huh?  If you knew it, cool, if not more explanation needed.  

Regular Expressions Tutorial.
http://www.regular-expressions.info/tutorial.html

Regular expressions are a very powerful parsing and validation tool that does the job quite nicely.  It is, unfortunately, very complex to understand at first.

Bob
0
 
Bob LearnedCommented:
At the very top of the code view add:
Imports System.Text.RegularExpressions


Add the following function:
  Public Function TestAddress(ByVal inputText As String) As Boolean

    Dim isValid As Boolean

    Try

      Dim patternAddress As String = "\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,15}"

      Dim addressTest As Regex = New Regex(patternAddress)

      isValid = addressTest.IsMatch(inputText)

    Catch ex As Exception

      MessageBox.Show(ex.ToString)

    End Try

    Return isValid

  End Function


Bob
0
 
gleznovAuthor Commented:
That turned out to be something a lot harder than I thought, and that reference is excellent, so I'm upping your reward.

JP

Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now