Checking string for special characters


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Checking string for special characters

  1. #1
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557

    Wink Checking string for special characters

    I hate to even ask this BUT......

    I've forgotten how to test a string for special characters. Will you PLEASE get me started in the right direction?

    THX!
    L

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Define "special character." Do you just want to know whether or not certain characters are present? Do you need to know their position within the string? Do you want to remove or replace them?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    I want to make sure that the string doesn't contain / \ : * ? " < > or |

    File names can not have these characters in them and I want to make sure that what the user types in the textbox doesn't contain these characters as a file will be created at some point with the textbox text.

    If they type in any of those characters, I want to pop up a message box telling them to correct the input.

    Make sense?

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    How about something like this:
    Code:
    Function ContainsIllegalChars(ByVal StringToTest As String, ByVal IllegalChars As String) As Boolean
    
        Dim Result As Boolean
    
        If Len(StringToTest) Then
            Dim I As Integer
            For I = 1 To Len(IllegalChars)
                Result = InStr(StringToTest, Mid$(IllegalChars, I, 1))
                If Result Then Exit For
            Next
        End If
        
        ContainsIllegalChars = Result
    
    End Function
    Call it like this:
    Code:
    If ContainsIllegalChars(FileName, "/\:*?""<>|") Then
        ' Yell at user
    End If
    I must add, however, that message boxes are evil! ;-) I'd much rather use an app that simply discards the illegal characters or disables the Save button until I enter a valid file name. Use a tooltip or other non-modal method to inform the user of a problem.
    Last edited by Phil Weber; 06-23-2004 at 10:27 PM.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    I know they're not pretty BUT......

    Thanks for the help, Phil ("The Man"). It's almost 9pm here now and I need to go home. I'll look at the code more in the morning.

    L

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center