Results 1 to 9 of 9

Thread: RegExp: Alphanumeric and Hypen Only?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    New Member
    Join Date
    Apr 2004
    Posts
    7
    ([0-9]*[a-z]*(-)*)+

    Theoretically, above regular expression shoud work, right?

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I tried this string: "a@sdf g-hj123leuf h gu" and it validated with that code.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    By the way, here is my exact code that I tried. Maybe I goofed something up:

    VB Code:
    1. Private Function ValidateVIN(ByVal strVinNumber) As String
    2.         Dim strNewVIN As String
    3.         Dim rEx As Regex
    4.  
    5.         'First thing we do is remove the spaces, since those don't count
    6.         'Towards our length requirements (ii):
    7.         strNewVIN = Replace(strVinNumber, " ", "")
    8.  
    9.         If Not rEx.IsMatch(strNewVIN, "([0-9]*[a-z]*(-)*)+") Then
    10.             Return "VIN Number " & strNewVIN & " contains invalid characters."
    11.         Else
    12.             Return "EVERYTHING IS GOOD!"
    13.         End If
    14.  
    15.     End Function
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    It might be easier to test for the allowed characters in one of the key events - keydown, keypress, etc.

  6. #6
    New Member
    Join Date
    Apr 2004
    Posts
    7
    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

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Here's a good online tester with library...
    http://regexlib.com/RETester.aspx

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.
    Code:
    ([^a-zA-Z0-9 -])
    Last edited by nemaroller; Apr 19th, 2004 at 07:35 PM.

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width