Solved

Find non-alphabetic char in string in VB

Posted on 2006-07-19
8
1,376 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

Independent Software Vendors: 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

718 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