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
Solved

Find non-alphabetic char in string in VB

Posted on 2006-07-19
8
1,372 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 142
has12 challenge 13 73
Add mobile access to browser application 3 199
How To Loop - Python 19 95
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 …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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