Link to home
Start Free TrialLog in
Avatar of Sheldonh
Sheldonh

asked on

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
Avatar of RonaldBiemans
RonaldBiemans

if System.Text.RegularExpression.RegEx.IsMatch(yourstring, "[a-zA-Z]")
msgbox ("ok")
else
  throw
End If
ASKER CERTIFIED SOLUTION
Avatar of S-Twilley
S-Twilley

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, too slow there :P
Avatar of Bob Learned
Actually, Steve, your expression will work, and Ronald's won't.  The concept of looking for non-alpha characters is a better approach.

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

but this should

  If System.Text.RegularExpressions.Regex.IsMatch("aa1aaa", "^[a-zA-Z]+$") Then
Avatar of Sheldonh

ASKER

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")
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
I see, your're probably right.

Well it is working now, I just need a little help with the spaces thing... no harm done... ;-)
what spaces thing? :\
my comment with the code attached above...
do you just want to allow one space within a valid string, or any number of spaces?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
any number of spaces
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
Thanks, Steve, I just beat you to the punch.  Must give credit where credit is due.

Bob
Thanks guys, you all helped alot.

Cheers
Sheldon