gleznov
asked on
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
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
ASKER
whooooooooah. Whaaaaaat?
JP
JP
ASKER
How do I go about implementing that?
JP
JP
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
At the very top of the code view add:
Imports System.Text.RegularExpress ions
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(inputT ext)
Catch ex As Exception
MessageBox.Show(ex.ToStrin g)
End Try
Return isValid
End Function
Bob
Imports System.Text.RegularExpress
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
Dim addressTest As Regex = New Regex(patternAddress)
isValid = addressTest.IsMatch(inputT
Catch ex As Exception
MessageBox.Show(ex.ToStrin
End Try
Return isValid
End Function
Bob
ASKER
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!
JP
Thanks!
Simple:
\d{1,3}).?\d{0,3}\s[a-zA-Z
Complex:
^(?n:(?<address1>(\d{1,5}(
)|(P\.O\.\ Box\ \d{1,5}))\s{1,2}(?i:(?<add
LDG|DEPT|FL|HNGR|LOT|PIER|
\x20(?<state>A[LKSZRAP]|C[
N]|K[SY]|LA|M[ADEHINOPST]|
|T[NX]|UT|V[AIT]|W[AIVY])\
{4})?))$
Bob