Link to home
Start Free TrialLog in
Avatar of raghx_2000
raghx_2000

asked on

Find non-alphabetic char in string in VB

Hi there
I want to verify whether a string has any non alphabetic character or not.
Flow goes like this:
str = "AD1@aYaZ"
If str has Non-Alphabetic character then
   Return True
Else str has no Non-Alphabetic Character then
  MsgBox "String must have 1 non-alphabetic character"
  Retun False
End if

Please help me, with code.
Avatar of bpmurray
bpmurray
Flag of Ireland image

The simplest solution is to use a regular expression. I presume this is enforcing some password complexity rules, so you can do something like:

       Dim Str As String = "AD1@aYaZ6"
        Dim re As New System.Text.RegularExpressions.Regex("^[A-Za-z]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        Dim m As System.Text.RegularExpressions.Match = re.Match(Str)
        If m.Success Then
            MsgBox("String must have 1 non-alphabetic character")
            Return False
        End If
        Return True
Avatar of raghx_2000
raghx_2000

ASKER

Thanks murray,
I know to do it in .NET way, but unfortunately iam using VB 3. So any user procedure / function etc.

hint: We got LIKE keyword similar to regEx, but i dont know to use it.

any suggestions.....
ASKER CERTIFIED SOLUTION
Avatar of bpmurray
bpmurray
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot - if the string can be any length, you have to really check character by character (I haven't checked this, so there may be a typo):

      For iX=0 To Str.length
           If Str.Char[iX] like "[!A-Za-z]" then
               return True
           End If
       Next
       Return False
Just do:

str = "AD1@aYaZ"
Dim i As Integer, token As Integer
For i = 1 To Len(str)
   token = Asc(Mid$(str, i, 1))
   If token < 65 Or (token > 90 And token < 97) Or token > 122 Then Return True
Next i
MsgBox "String must have 1 non-alphabetic character"
Return False
Oh, you have to substitute the stupid return mechanism for "Return."  I forgot.  So it'd be fooFighterFnc = True: Exit Function, etc.