Solved

Find non-alphabetic char in string in VB

Posted on 2006-07-19
8
1,361 Views
Last Modified: 2008-01-16
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.
0
Comment
Question by:raghx_2000
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 17138165
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
0
 
LVL 3

Author Comment

by:raghx_2000
ID: 17144721
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.....
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 100 total points
ID: 17144769
OK, use the like operator in plain ol' VB. If we assume the string must be 8 characters, and have at least one non-alphabetic character, you can use:

        Dim Str As String = "AD1@aYaZ6"
       
        If Str like "[A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z]" then
            MsgBox("String must have 1 non-alphabetic character")
            Return False
        End If
        Return True
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Expert Comment

by:bpmurray
ID: 17144792
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
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17156621
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
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 17156645
Oh, you have to substitute the stupid return mechanism for "Return."  I forgot.  So it'd be fooFighterFnc = True: Exit Function, etc.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now