Solved

Find non-alphabetic char in string in VB

Posted on 2006-07-19
8
1,375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factory design pattern vs abstract factoy design pattern 2 128
drawing animated level bar based on numbers 3 121
Capture logon name 13 119
Link failure 16 89
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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