|
-
Apr 19th, 2004, 03:52 PM
#1
Thread Starter
Stuck in the 80s
RegExp: Alphanumeric and Hypen Only?
How can I use regular expressions to make sure a string only has letters, numbers, and/or hypens?
0123 - Okay
0a12 - Okay
0-12 - Okay
Any other characters are not allowed. I've searched for this, but all I've found is confusion. Any help would be appreciated.
-
Apr 19th, 2004, 04:12 PM
#2
New Member
([0-9]*[a-z]*(-)*)+
Theoretically, above regular expression shoud work, right?
-
Apr 19th, 2004, 04:18 PM
#3
Thread Starter
Stuck in the 80s
I tried this string: "a@sdf g-hj123leuf h gu" and it validated with that code.
-
Apr 19th, 2004, 04:19 PM
#4
Thread Starter
Stuck in the 80s
By the way, here is my exact code that I tried. Maybe I goofed something up:
VB Code:
Private Function ValidateVIN(ByVal strVinNumber) As String
Dim strNewVIN As String
Dim rEx As Regex
'First thing we do is remove the spaces, since those don't count
'Towards our length requirements (ii):
strNewVIN = Replace(strVinNumber, " ", "")
If Not rEx.IsMatch(strNewVIN, "([0-9]*[a-z]*(-)*)+") Then
Return "VIN Number " & strNewVIN & " contains invalid characters."
Else
Return "EVERYTHING IS GOOD!"
End If
End Function
-
Apr 19th, 2004, 05:01 PM
#5
Frenzied Member
It might be easier to test for the allowed characters in one of the key events - keydown, keypress, etc.
-
Apr 19th, 2004, 05:27 PM
#6
New Member
Hope this solves your problem.
Shashi
Private Function ValidateVIN(ByVal strVinNumber As String) As String
Dim strNewVIN As String
Dim rEx As New System.Text.RegularExpressions.Regex("[^A-Za-z0-9-]+")
'First thing we do is remove the spaces, since those don't count
'Towards our length requirements (ii):
strNewVIN = Replace(strVinNumber, " ", "")
If rEx.Match(strNewVIN).Success Then
Return "VIN Number " & strNewVIN & " contains invalid characters."
Else
Return "EVERYTHING IS GOOD!"
End If
-
Apr 19th, 2004, 06:40 PM
#7
I wonder how many charact
Here's a good online tester with library...
http://regexlib.com/RETester.aspx
-
Apr 19th, 2004, 07:25 PM
#8
I wonder how many charact
Anyway I think this should do it...
It does the complete opposite... MATCHES WHEN INVALID characters exist.
Notice there is a space between 9 and _ in there.. I wasn't sure if you were allowing spaces or not, so if you're not, remove the space.
Last edited by nemaroller; Apr 19th, 2004 at 07:35 PM.
-
Apr 19th, 2004, 10:38 PM
#9
Thread Starter
Stuck in the 80s
Originally posted by salvelinus
It might be easier to test for the allowed characters in one of the key events - keydown, keypress, etc.
There are no key events. The data is read from a file created by another program.
To everyone else: I'll give those a try, thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|