类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
有谁可以给我个简单的类,来检查用户文本框: 1)是否为空
2)长度是否超过了自定义的长?
3)只可以输入汉字
4)只可以输入字母
5)只可以输入数字
6)当文本框既有汉字,又有字母时候他的长度是多少?
好,就这么多吧,我自己写了一个,但就是不调用,哪为高人帮我指点一个思路??
网友回答:
给你一个例子,可以设置只允许输入数字,或只允许输入字母。其它的条件你自己去扩展。
类名cTxt
public withevents textbox as textbox
public isDecimal as boolean 是,则只能输入数字,否,则只能输入字母
private sub textbox_keypress(keyascii as integer)
select case keyascii
case 0 to 31
case 48 to 57 数字
if not isdecimal then keyascii=0
case 65 to 90,97 to 122 字母
if isdecimal then keyascii=0
end select
end sub
测试,在窗体中放两个文本框,text1,text2
dim deciText as ctxt,letterText as ctxt
private sub form_load()
set decitext=new ctxt
set decitext.textbox=text1
decitext.isdecimal=true
set lettertext=new ctxt
set letterText.textbox=text2
letterText.isdecimal=false
end sub
不需要类吧,写几个函数就可以了啊
1、
If Trim(text1.text)="" then msgbox "空"
2、
if len(text1.text)>自定义的长度 then msgbox "太长了"
3、这个问题有点复杂
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Len(Hex$(KeyAscii)) <= 2 And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
4、只可以输入字母
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 65 Or KeyAscii > 122) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
4、只可以输入数字
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
5、混合长度
LenB(StrConv(Text1.text, vbFromUnicode))
用函数就行:
Public Function pub_ajpd(cs_ascii As Integer, cs_tj As Integer, cs_jg As Boolean) 按键判断
此函数用于判断用户所输入每一字符的合法性,用大文本控件的keypress事件中。
此参数代表keypress事件中的ascii参数
cs_tj 表示输入数据的类型 0--字符型(任意字符),1---数字型(数量,金额),2--日期型,3---拼音简码,4---ID
cs_tj 如果所输入字符合法,就返回真值
Select Case cs_tj
Case 0
cs_ascii = 0
cs_jg = False
Case 1
If (cs_ascii >= 48 And cs_ascii <= 57) Or cs_ascii = 45 Or cs_ascii = 46 Or cs_ascii = 13 Or cs_ascii = 8 Then
cs_jg = True
Else
cs_ascii = 0
cs_jg = False
End If
Case 2
If cs_ascii >= 48 And cs_ascii <= 57 Or cs_ascii = 13 Or cs_ascii = 8 Then
cs_jg = True
Else
cs_ascii = 0
cs_jg = False
End If
Case 3
If (cs_ascii >= 65 And cs_ascii <= 90) Or (cs_ascii >= 97 And cs_ascii <= 122) Or cs_ascii = 8 Or cs_ascii = 13 Then
cs_jg = True
Else
cs_ascii = 0
cs_jg = False
End If
Case 4
If (cs_ascii >= 48 And cs_ascii <= 57) Or cs_ascii = 8 Or cs_ascii = 13 Then
cs_jg = True
Else
cs_jg = False
End If
End Select
End Function
我这里有一段类似的代码。它可以用于用户提交注册信息中EMAIL的合法性检测。
Private Function CheckData() As Boolean
If Len(Trim(EmailInputBox.Text)) < 7 Or Len(Trim(EmailInputBox.Text)) > 40 Then
‘对于一个合法的EMAIL地址,它必定是此种格式:X@Y.COM,长度最少为7位。
‘同时保存用户信息的数据库字段长度为40个字符。
长度不符合标准时,向用户提出警告!
End If
If InStr(1, EmailInputBox.Text, "=") Or InStr(1, EmailInputBox.Text, "!")
Then
‘不能出现“=”,“!”这类跟逻辑运算相关的字符。
输入了非法数据时,向用户提出警告!
End If
If InStr(1, EmailInputBox.Text, "@") = 0 Or InStr(1, EmailInputBox.Text, ".") = 0 Then
‘EMAIL地址中一定会有@和.这两个字符。
格式不正确时,向用户提出警告!
End If
‘向数据库添加用户信息
End Function
基本上能实现你的要求,具体代码你自己改改吧。如果还有问题,联系我:zhaodacheng111@163.com
if len(text1.text)=0 then msgbox"为空",vbokonly 第一个问题
if len(text1.text)> text1.maxlength then msgbox"长度超过设置的长度" 第二个问题
Private Sub Text1_KeyPress(KeyAscii As Integer) 第三个问题
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> 13 Then KeyAscii = 0
’这里是限制只能输入数字和使用退格以及回车,四,五就自己去改那些数字咯
End Sub