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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops, too slow there :P
Actually, Steve, your expression will work, and Ronald's won't. The concept of looking for non-alpha characters is a better approach.
Bob
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.RegularExpress ions.Regex .IsMatch(" aa1aaa", "^[a-zA-Z]+$") Then
but this should
If System.Text.RegularExpress
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.RegularExpress ions.Regex ("[^a-zA-Z ]")
If RegTest.IsMatch(TxtNames.T ext) Then
MsgBox("Kak", MsgBoxStyle.Exclamation, "Message")
e.Cancel = True
Else
MsgBox("Success", MsgBoxStyle.Information, "Message")
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.RegularExpress
If RegTest.IsMatch(TxtNames.T
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.RegularExpress ions
(I did mention it)
unless you didn't like the If Else Block
Imports System.Text.RegularExpress
(I did mention it)
unless you didn't like the If Else Block
ASKER
I see, your're probably right.
Well it is working now, I just need a little help with the spaces thing... no harm done... ;-)
Well it is working now, I just need a little help with the spaces thing... no harm done... ;-)
what spaces thing? :\
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).S uccess 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
Dim regTest As New Regex("[^a-zA-Z\s]")
...
myTestStr = "kjhsdkg98797sdfd"
If regTest.Match(myTestStr).S
'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
Bob
ASKER
Thanks guys, you all helped alot.
Cheers
Sheldon
Cheers
Sheldon
msgbox ("ok")
else
throw
End If