关于输入数据的检查问题,请大家帮忙,谢谢!!!
我不想让用户在TEXTBOX中输入'或",请问该怎么办,不会给每个TEXTBOX都判断KEYPRESS事件吧,请大家帮忙啊,另外还有一个帖子也请看一看,谢谢!!!
http://community.csdn.net/Expert/topic/3137/3137273.xml?temp=.7093622
问题点数:100、回复次数:23Top
1 楼zjsen(位高权重责任轻,钱多事少离家近,睡觉睡到自然醒, 数钱数到手抽筋. )回复于 2004-07-02 12:43:39 得分 5
正则表达式
[^\"]|[^']Top
2 楼jonescheng(小块头无大智慧)回复于 2004-07-02 12:57:43 得分 20
function chkNonChar(obj){
var strInput;
var strNonChar = new Array();
strInput = obj.value;
//允许加用以下字符,请在前加上“//”
strNonChar[0] = "\'"; //禁用'
strNonChar[1] = "\""; //禁用"
strNonChar[2] = "\\"; //禁用 strNonChar[3] = "~"; //禁用~
strNonChar[4] = "#"; //禁用#
strNonChar[5] = "$"; //禁用$
strNonChar[6] = "%"; //禁用%
strNonChar[7] = "^"; //禁用^
strNonChar[8] = "&"; //禁用&
strNonChar[9] = "*"; //禁用*
strNonChar[10] = "<"; //禁用<
strNonChar[11] = ">"; //禁用>
strNonChar[12] = "/"; //禁用/
strNonChar[13] = "?"; //禁用?
strNonChar[14] = "|"; //禁用|
strNonChar[15] = "+"; //禁用+
strNonChar[16] = "`"; //禁用`
if (strInput == '') return true;
//在输入的参数中,查找是否有系统定义非法字符
for (var i = 0;i < strNonChar.length; i++){
if ( strInput.indexOf(strNonChar[i]) > -1 ) {
alert('【系统提示】\n\n输入的文字中含有本系统所不允许的非法字符“' + strNonChar[i] + '” !\n\n请重新输入或换用别的字符替代。')
obj.focus();
obj.select();
return false;
}
}
return true;
}Top
3 楼VCILOVE(VCILOVE)回复于 2004-07-02 13:12:12 得分 0
那是要对每一个都调用了,那多麻烦啊,有没有什么拦截消息的方法啊,谢谢Top
4 楼VCILOVE(VCILOVE)回复于 2004-07-02 14:06:00 得分 0
怎么还是没有人来回答呢Top
5 楼terryxin(南方窗口)回复于 2004-07-02 14:25:09 得分 0
可能没有更好的方法,帮你顶,看有人会没有Top
6 楼VCILOVE(VCILOVE)回复于 2004-07-03 09:32:50 得分 0
还是没有人来回答吗Top
7 楼lzmtw(水如烟)回复于 2004-07-03 10:06:13 得分 0
我的方法好笨,贴出来就惭愧了Top
8 楼loveerror(马烁焱)回复于 2004-07-03 10:19:14 得分 5
重写textbox类呀.
然后在输入里筛选.Top
9 楼AntingZ(夕惕若)回复于 2004-07-03 10:22:15 得分 0
重写TextBox控件如何Top
10 楼starrow()回复于 2004-07-03 10:24:54 得分 10
不必为每个textbox都写事件相应代码啊,只要写一个过程,然后为每个textbox都加上这个事件处理程序。比如在form的自带InitializeComponents过程中加入
dim txtBox as textbox,ctrl as control
for each ctrl in me.controls
txtBox=directcast(ctrl,textbox)
addhandler txtBox.keypress ,addressof textbox_keypress
next
'textbox_keypress就是写好的解决剔除特定字符输入的过程Top
11 楼lzmtw(水如烟)回复于 2004-07-03 10:45:29 得分 20
呵,还是贴出来吧,不过对系统不知有什么影响就不清楚了(哎,惭愧,乱来,胡闹):
Private NoChars() As Char = New Char() {"A", "a", "或"}
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As Message)
If TypeOf Me.ActiveControl Is TextBox Then
Dim bt As TextBox = CType(Me.ActiveControl, TextBox)
Dim mChar As Char
For Each mChar In bt.Text.ToCharArray
If Array.IndexOf(NoChars, mChar) > -1 Then
bt.Text = bt.Text.Replace(mChar, "")
bt.SelectionStart = bt.TextLength
End If
Next
End If
MyBase.WndProc(m)
End SubTop
12 楼lzmtw(水如烟)回复于 2004-07-03 10:59:27 得分 0
TextBox.TextChanged的Message是什么,知道了上头的就可以有选择的处理Message了
(也学会懒了,自己不去查,沾了CSDN的陋习)Top
13 楼VCILOVE(VCILOVE)回复于 2004-07-03 11:18:41 得分 0
我就是想拦截MESSAGE,只要是在我的FORM中KEYPRESS,那我就可以做处理了,但是现在就是不知道该怎么办,菜鸟没有办法啊,请大家帮忙啦,谢谢Top
14 楼lzmtw(水如烟)回复于 2004-07-03 11:23:15 得分 0
呵,我上面的代码你运行过没有,完全达到你的要求,只是有什么弊病我不大清楚罢了Top
15 楼lzmtw(水如烟)回复于 2004-07-03 11:24:18 得分 0
是Overrides你的Form的Top
16 楼lzmtw(水如烟)回复于 2004-07-03 14:45:02 得分 0
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(224, 40)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(256, 20)
Me.ComboBox1.TabIndex = 0
Me.ComboBox1.Text = "ComboBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(504, 40)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(200, 120)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 21)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(164, 172)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(248, 21)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(184, 240)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(248, 21)
Me.TextBox3.TabIndex = 4
Me.TextBox3.Text = "TextBox3"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(576, 365)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.ComboBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private NoChars() As Char = New Char() {"A", "a", "或"}
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As Message)
If TypeOf Me.ActiveControl Is TextBox Then
Dim bt As TextBox = CType(Me.ActiveControl, TextBox)
Dim mChar As Char
For Each mChar In bt.Text.ToCharArray
If Array.IndexOf(NoChars, mChar) > -1 Then
bt.Text = bt.Text.Replace(mChar, "")
bt.SelectionStart = bt.TextLength
End If
Next
End If
MyBase.WndProc(m)
End Sub
End ClassTop
17 楼VCILOVE(VCILOVE)回复于 2004-07-03 15:57:28 得分 0
谢谢lzmtw(水如烟)
就去试Top
18 楼lzmtw(水如烟)回复于 2004-07-03 15:58:57 得分 0
原来是这样的:
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0xffffffffdc011b02 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x1000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0xffffffffdc011b02 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:297 ,EventName:msg=0x129 hwnd=0x5d026c wparam=0x0 lparam=0x0 result=0x0
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0x5c011227 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a800ec result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:49790 ,EventName:msg=0xc27e hwnd=0x5d026c wparam=0x0 lparam=0x0 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf7004e result=0x0
ControlName:TextBox3 ,Msg:673 ,EventName:msg=0x2a1 (WM_MOUSEHOVER) hwnd=0x5d026c wparam=0x0 lparam=0xf7004e result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x4000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0x5c011227 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x3000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x4000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0x5c011227 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x3000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x4000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:307 ,EventName:msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x5d026c wparam=0x5c011227 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:273 ,EventName:msg=0x111 (WM_COMMAND) hwnd=0x5d026c wparam=0x3000290 lparam=0x8c0290 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a800ed result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf7004f result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a800ee result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf70050 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a700ef result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf60051 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a700f0 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf60052 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a700f1 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf60053 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a600f2 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf50054 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a600f4 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf50056 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a500f7 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf40059 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1a300fa result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xf2005c result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x19f0102 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xee0064 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x19a010e result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xe90070 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1900123 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xdf0085 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x1850142 result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xd400a4 result=0x0
ControlName:TextBox3 ,Msg:132 ,EventName:msg=0x84 (WM_NCHITTEST) hwnd=0x5d026c wparam=0x0 lparam=0x175016f result=0x0
ControlName:TextBox3 ,Msg:32 ,EventName:msg=0x20 (WM_SETCURSOR) hwnd=0x5d026c wparam=0x5d026c lparam=0x2000001 result=0x0
ControlName:TextBox3 ,Msg:512 ,EventName:msg=0x200 (WM_MOUSEMOVE) hwnd=0x5d026c wparam=0x0 lparam=0xc400d1 result=0x0
ControlName:TextBox3 ,Msg:675 ,EventName:msg=0x2a3 (WM_MOUSELEAVE) hwnd=0x5d026c wparam=0x0 lparam=0x0 result=0x0
Top
19 楼lzmtw(水如烟)回复于 2004-07-03 16:01:26 得分 0
呵,这样做不知道有什么弊处,会不会大大降低系统性能呢
要不,好多东西在这里就可以解决的了Top
20 楼justok([热心-冲星])回复于 2004-07-03 16:10:28 得分 20
办法1,重写TEXTBOX,然后把需要控制的用你写的TEXTBOX代替
办法2,把多个事件绑定到一个事件函数上,在事件函数后面Handles TEXTBOX1.KEYPRESS,TEXTBOX2。KEYPRESS,TEXTBOX3。KEYPRESSTop
21 楼lzmtw(水如烟)回复于 2004-07-03 16:40:13 得分 0
发现楼主提的问题不少哪Top
22 楼justok([热心-冲星])回复于 2004-07-03 18:45:53 得分 15
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click,Button3.Click
MsgBox(1)
End Sub
你把3个BUTTON放到窗体上去,把我的代码复制进去,你随便按那一个按钮都会执行代码,你那个TEXTBOX就同理类退,不明白再发消息给我Top
23 楼dgz988(雨潇)回复于 2004-07-04 09:30:34 得分 5
晕啊。。。朋友你还是每个都用KEYPRESS吧,,哈哈。。。好麻烦啊。。
不过KEYPRESS好像有个事件的简单写法,把所有TEXTBOX写到到一个事件中,用逗号分开。。Top
相关问题
- 客户端输入数据检查(很简单)
- 请问: 在数据窗口的什么 event里来检查刚刚输入的数据,并把该数据用 setitem的办法写到别的 item 中, 用itemchanged 好象不太好, 因为当我点数据窗口其他没有修改权的item时,我刚刚输入的数据并没有被接受。
- 数据输入
- 如何检查Text中输入的是正确的日期,即数据的合法性检查?
- 怎么样解决数据窗口的字段值在输入的时候检查字符有效性?请大家帮帮忙!非常感谢!!!
- 请大家帮我看看错在哪了?在脚本中检查用户输入的值在数据库中是否存在
- 请问如何向Oracle数据表中输入数据?
- 请教通过ADO控件向数据库输入数据
- 数据输入数据库出错,请教高手
- 请问:在数据窗口中输入多个数据后,用什么办法得到输入前的数据和输入后的数据




