Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a bit of code I use to make sure that invalid special characters do not make it into a textbox in a windows form project. Users often seem to copy/paste info into this textbox from Microsoft Word and it was becoming a problem when they would paste bullets or characters that Word auto-changes like hyphens and elipses. It works fine in development and we had no problems with it in testing. Now I have a customer who is able to get bullets to paste into it. It doesn't cause any errors, but it doesn't convert the bullet to an asterisk like I'm expecting it to.

The customer in question is known for having very tight security. The situation made me think about how some anti-virus applications will block SendKeys from working in programs. Does anyone know if this is also an issue for things like a .Replace using regular expressions? If so, are there any alternative methods to accomplish my end goal? Or can anyone find a reason why this wouldn't work?

Here are the event and method I use to keep only valid characters in the textbox:
 Private Sub txtSpecialReport_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSpecialReport.TextChanged
     'Only allow known characters to be pasted or typed into txtSpecialReport to avoid Conversion Error on characters
     'that are okay in access db but not on iSeries when claim is uploaded.
     Dim intStart As Integer = txtSpecialReport.SelectionStart
     '^negates the whole thing (we want ones that DON'T match)
     '\w is for a-zA-Z0-9
     '\s is for spaces, tabs, returns
     '\!\@\#\$\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\"" are allowed special characters
     Dim strPattern As String = "[^\w\s\!\@\#\$\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\""]"
     Dim myRegex As New System.Text.RegularExpressions.Regex(strPattern)
     txtSpecialReport.Text = myRegex.Replace(txtSpecialReport.Text, New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceKnownSpecials))
     txtSpecialReport.SelectionStart = intStart
 End Sub

 Private Function ReplaceKnownSpecials(ByVal match As System.Text.RegularExpressions.Match) As String
    Select Case Asc(match.Value(0))
        Case 63, 146
            Return "'" 'Replace an ascii 63 or 146 with ascii 39
        Case 133
            Return "." 'Replace an ascii 133 elipses with a single period (Not three (...) because then it changes string length)
        Case 150
            Return "-" 'Replace an ascii 150 dash with an ascii 45 dash
    End Select
    Return "*" 'Relace any other unknown char with an asterisk
End Function

1 solution

I've never heard of regexes being blocked: I'm not convinced they could be - all they are is a function that changes text strings.

Are you sure they are pasting illegal characters? It's not coming is as (say) space-dot-space or similar?
Share this answer
Kschuler 24-May-11 8:44am    
Yes it's an illegal caused an error on our system later which is why I needed the code in the first place. We had the user copy/paste what they had used and send it to us in an email and it's for sure a bullet character. ASCII code 149.
OriginalGriff 24-May-11 9:13am    
Can you duplicate the problem locally?
I tried it here (after running your code though a VB->C# converter, I don't have a VB project handy) and it passed the two illegals from "1•2¬3" to the match evaluator fine.
Can they paste or type the '¬' character in?
I doubt it's relevant but are you all using the same locale?
Kschuler 24-May-11 15:23pm    
I haven't been able to reproduce the issue on my machine. Since the code is in the TextChanged event it is supposed to handle typing or copying of the invalid special characters. What do you mean by locale? If you mean physical The user with the error works for a different company in a different state. If you mean a setting I do not really have immediate access to any of his settings. He's not employed with my company.
OriginalGriff 24-May-11 15:30pm    
If it's a different state, but the same country then locale is unlikely to be a problem: locale affects keyboard layout and characters but since word and .NET are both Unicode it shouldn't cause any problems even it there was an oddity - I was running out of ideas!
It's always a pain when they don't work for your company and you can't replicate the problem.
If it has only happened once, then I would be tempted to put it down to finger trouble and wait until it occurs again - if you can't duplicate it, you can't fix it!
Kschuler 24-May-11 15:38pm    
I can't replicate it...but the user does. Our support dept. did an on-line meeting with the guy and we can see that he can paste the bullet character when it should convert it to an asterisk. So I'm afraid others from his company might have the same issue. But you're right...even if I narrow down the issue and figure out which part is broken I have no idea how to do it differently or fix it. Thanks for the ideas.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900