文本框验证的方法 [英] Method for textbox validation
问题描述
我有一个Windows窗体应用程序,该应用程序有一个文本框,供用户输入有关其工作的特殊注释.我们遇到了麻烦,因为用户一直在使用Microsoft Word键入这些注释,然后他们将它们复制/粘贴到我的应用程序中.这是一个问题,因为例如,如果在Word中键入...,它将转换为椭圆字符.另外,如果您键入-它可以将其转换为连字符.这些可以很好地粘贴到文本框中,但是最终这些数据将被保存到不喜欢这些字符的iSeries中.因此,我整理了一些代码,它们既可以作为用户类型使用,也可以作为用户使用复制/粘贴功能将代码替换为对应的麻烦字符,并使用星号替换所有未知字符的代码.它有效,但是我感觉必须有一种更好,更有效的方法来做到这一点.有人有什么建议吗?
I have a windows forms application that has a textbox for users to enter special comments about their work. We''ve been having trouble because users have been using Microsoft Word to type these comments and then they copy/paste them into my application. It''s a problem because, for example, in Word if you type ... it converts it to an elipse character. Also if you type - it can convert it into a hyphen character. These are fine to paste into the textbox, but eventually this data is getting saved to an iSeries which doesn''t like these characters. So I put together some code which should work both as the user types and if they use copy/paste to replace known trouble characters with counterparts and any unkown characters with an asterisk. It works, but I have a feeling there must be a better and more efficient way to do it. Does anyone have any suggestions?
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 errors on iSeries
'\w is for a-zA-Z0-9, \s is for spaces, tabs, returns, the rest are allowed special characters
Dim myRegex As New System.Text.RegularExpressions.Regex("[\w\s\!\@\#\$\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\""]")
Dim sb As New System.Text.StringBuilder
Dim intStart As Integer = txtSpecialReport.SelectionStart
For Each myChar As Char In txtSpecialReport.Text.ToCharArray
If Not myRegex.IsMatch(myChar) Then
'known replacements
Select Case Asc(myChar)
Case 63
'Replace an ascii 63 with ascii 39
myChar = "'"
Case 133
'Replace an ascii 133 elipses with a period
'don't want to change the length of the string, so don't replace with three periods
myChar = "."
Case 146
'Replace an ascii 146 with ascii 39
myChar = "'"
Case 150
'Replace an ascii 150 dash with an ascii 45 dash
myChar = "-"
Case Else
'Any other characters replace with an asterisk
myChar = "*"
End Select
End If
sb.Append(myChar)
Next
txtSpecialReport.Text = sb.ToString
txtSpecialReport.SelectionStart = intStart
End Sub
推荐答案
\%\ ^ \& \ * \(\)\-\ == ++ [\] \ {\} \\\ | \; \:\'\,\< \.\> \//\?\"]") Dim sb As New System.Text. StringBuilder Dim intStart As 整数 = txtSpecialReport.SelectionStart 对于 每个 myChar As 字符 在中txtSpecialReport.Text.ToCharArray 如果 不是 myRegex.IsMatch(myChar)然后 ' 已知替代品 选择 案例 Asc(myChar) 案例 63 ' 用ascii 39替换ascii 63 myChar = " 案例 133 ' 用时间段替换ASCII 133椭圆 ' 不想更改字符串的长度,因此请勿替换为三个句点 myChar = " 案例 146 ' 将ascii 146替换为ascii 39 myChar = " 案例 150 ' 用ascii 45破折号代替ascii 150破折号 myChar = " 案例 其他 ' 其他任何字符都替换为星号 myChar = " 结束 选择 结束 如果 sb.Append(myChar) 下一步 txtSpecialReport.Text = sb.ToString txtSpecialReport.SelectionStart = intStart 结束 子
\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\""]") Dim sb As New System.Text.StringBuilder Dim intStart As Integer = txtSpecialReport.SelectionStart For Each myChar As Char In txtSpecialReport.Text.ToCharArray If Not myRegex.IsMatch(myChar) Then 'known replacements Select Case Asc(myChar) Case 63 'Replace an ascii 63 with ascii 39 myChar = "'" Case 133 'Replace an ascii 133 elipses with a period 'don't want to change the length of the string, so don't replace with three periods myChar = "." Case 146 'Replace an ascii 146 with ascii 39 myChar = "'" Case 150 'Replace an ascii 150 dash with an ascii 45 dash myChar = "-" Case Else 'Any other characters replace with an asterisk myChar = "*" End Select End If sb.Append(myChar) Next txtSpecialReport.Text = sb.ToString txtSpecialReport.SelectionStart = intStart End Sub
不幸的是,AFAIK有top绝对不能用正则表达式将其中一个"替换为其中一个".
您可以使用MatchEvaluator消除对String构建器的需要,从而对其进行一些整理.使正则表达式只是已知无效字符的列表,并且:
Unfortunately, AFAIK there is no way top make a regex which replaces "one of these" with "one of those".
You can clean it up a bit, by using a MatchEvaluator to remove the need for a String builder. Make the regex just a list of known invalid characters and:
result = myRegex.Replace(input, New MatchEvaluator(AddressOf ReplaceSpecials))
Private Function ReplaceSpecials(match As Match) As String
Select Case Asc(match.Value(0))
Case 63
Return "''"
Case 133
Return "."
End Select
Return "*"
End Function
未经测试,但您有一个大致的概念:请参阅 MSDN [ ^ ]了解更多详细信息.
-KSCHULER 添加了缺少"AddressOf"关键字以更正语法,并删除了该关键字;
This isn''t tested, but you get the general idea: See MSDN[^] for better details.
-- KSCHULER Added the "AddressOf" keyword that was missing to correct syntax and removed ;
这篇关于文本框验证的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!