String Validation

Hi

How can I validate a string which only allows these characters... A - Z , a-z, if other than those characters are detected then I want to throw an exception.

Thanks
Sheldon
LVL 1
SheldonhAsked:
Who is Participating?
 
S-TwilleyCommented:
Imports System.Text.RegularExpressions
------------------------------------------------------

Then in your sub...

Dim regTest As New Regex("[^a-zA-Z]")

...

myTestStr = "kjhsdkg98797sdfd"

If regTest.Match(myTestStr).Success then  
      'contains bad chars
Else
       'it's valid
End If
0
 
RonaldBiemansCommented:
if System.Text.RegularExpression.RegEx.IsMatch(yourstring, "[a-zA-Z]")
msgbox ("ok")
else
  throw
End If
0
 
S-TwilleyCommented:
oops, too slow there :P
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Bob LearnedCommented:
Actually, Steve, your expression will work, and Ronald's won't.  The concept of looking for non-alpha characters is a better approach.

Bob
0
 
S-TwilleyCommented:
I didn't even notice. Well, I'm sure the thought was there... easy slip-up for someone to make :P
0
 
RonaldBiemansCommented:
you are right TheLeanedOne (as usual :-)

but this should

  If System.Text.RegularExpressions.Regex.IsMatch("aa1aaa", "^[a-zA-Z]+$") Then
0
 
SheldonhAuthor Commented:
Hi

I have combined your techniques to come up with this solution...the ones that you guys provided didn't work.

So, here is another question. What if I enter a person's name and there is a space...how would I accomodate the space?




Dim RegTest As New System.Text.RegularExpressions.Regex("[^a-zA-Z]")

If RegTest.IsMatch(TxtNames.Text) Then

MsgBox("Kak", MsgBoxStyle.Exclamation, "Message")

e.Cancel = True

Else

MsgBox("Success", MsgBoxStyle.Information, "Message")
0
 
S-TwilleyCommented:
if you had a problem with mine... it's probably because you didn't add the import statement at the top of your class:

Imports System.Text.RegularExpressions

(I did mention it)

unless you didn't like the If Else Block
0
 
SheldonhAuthor Commented:
I see, your're probably right.

Well it is working now, I just need a little help with the spaces thing... no harm done... ;-)
0
 
S-TwilleyCommented:
what spaces thing? :\
0
 
SheldonhAuthor Commented:
my comment with the code attached above...
0
 
S-TwilleyCommented:
do you just want to allow one space within a valid string, or any number of spaces?
0
 
Bob LearnedCommented:
The space character = \s

So [^a-zA-Z\s]+

Bob
0
 
SheldonhAuthor Commented:
any number of spaces
0
 
S-TwilleyCommented:
Bob's regex should do it... and im not sure, but you might also want to check the length of  Trim(myTestStr) to see if it's 0... which means that the entire string was full of spaces... so something like:

Dim regTest As New Regex("[^a-zA-Z\s]")

...

myTestStr = "kjhsdkg98797sdfd"

If regTest.Match(myTestStr).Success then  
      'contains bad chars
Else
        If Trim(myTestStr).Length = 0 Then
               'myTestStr was either empty, or just all spaces
         Else
               'myTestStr contains at least some letters, but possible spaces too
         End If
End If

============

Give credit to Bob though, since he did provide the Regex for spaces before me
0
 
Bob LearnedCommented:
Thanks, Steve, I just beat you to the punch.  Must give credit where credit is due.

Bob
0
 
SheldonhAuthor Commented:
Thanks guys, you all helped alot.

Cheers
Sheldon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.