[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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
0
Sheldonh
Asked:
Sheldonh
  • 7
  • 5
  • 3
  • +1
2 Solutions
 
RonaldBiemansCommented:
if System.Text.RegularExpression.RegEx.IsMatch(yourstring, "[a-zA-Z]")
msgbox ("ok")
else
  throw
End If
0
 
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
 
S-TwilleyCommented:
oops, too slow there :P
0
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!

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now