有谁可以给我个简单的类,来检查用户文本框?????

tiandiqing 2004-10-14 04:21:19
有谁可以给我个简单的类,来检查用户文本框: 1)是否为空
2)长度是否超过了自定义的长?
3)只可以输入汉字
4)只可以输入字母
5)只可以输入数字
6)当文本框既有汉字,又有字母时候他的长度是多少?


好,就这么多吧,我自己写了一个,但就是不调用,哪为高人帮我指点一个思路??
...全文
181 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
heyang714 2004-10-15
  • 打赏
  • 举报
回复
楼上的说得好!
设置他的MaxLength
GGL123 2004-10-15
  • 打赏
  • 举报
回复
上面的 If a = False Then 应为if a=1 then 特此更正。
GGL123 2004-10-15
  • 打赏
  • 举报
回复
在你的基础上可以这样改。

Public Function strLenght(ByVal str As String) As Integer
If Len(str) = 0 Then
strLenght = 1
Exit Function
End If

If Len(str) > 16 Then
strLenght = 2
Exit Function
End If

If Len(str) < 1 Then
strLenght = 3
Exit Function
End If
End Function


Private Sub Command1_Click()
Dim username As String
Dim ckuid As check_class
Dim pwd1 As String
Dim pwd2 As String
Dim message As String
Set ckuid = New check_class
Dim a As Integer

username = Form1.username.Text
pwd1 = Form1.pwd1.Text
pwd2 = Form1.pwd2.Text

a = ckuid.strLenght(username)
If a = False Then
message = MsgBox("提示用户名不能为空!", vbInformation + vbOKOnly, " 警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
ElseIf a = 2 Then
message = MsgBox("提示:用户名名称过长!", vbInformation + vbOKOnly, "警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If

'If ckuid.strLenght(username) Then
' message = MsgBox("提示:用户名名称过长!", vbInformation + vbOKOnly, "警告")
' Form1.username.Text = ""
' Form1.username.SetFocus
' Exit Sub
'End If
End Sub
tiandiqing 2004-10-15
  • 打赏
  • 举报
回复
victorycyz(中海)

我想问一下

正常应怎么写,我怎么调试他都显示用户名不能为空!
explore_xu 2004-10-15
  • 打赏
  • 举报
回复
想一想
victorycyz 2004-10-15
  • 打赏
  • 举报
回复

你在类里声明strlenght为boolean,也就是说,只能判断“合法”或“不合法”,不能准确判断是3种情况中的哪一种。你在后面调用时,也用了两次判断,If ckuid.strLenght(username) Then,这本身也是不合理的做法。看你的判断语句里的内容,如果第一次判断为“不合法”时,第二个判断是不会执行的。

再次提醒你,你的面向对象的思路也不地道啊。
GGL123 2004-10-15
  • 打赏
  • 举报
回复
//==================================
//我改过来它也不执行呀!!!

//错在哪里了???

应该这样调用,另外你的代码还有问题,请自查。不过这样就可以调用它了。

Private Sub Command1_Click()
Dim username As String
Dim ckuid As check_class
Dim pwd1 As String
Dim pwd2 As String
Dim message As String
Set ckuid = New check_class
Dim a As Boolean

username = Form1.username.Text
pwd1 = Form1.pwd1.Text
pwd2 = Form1.pwd2.Text

a = ckuid.strLenght(username)
If a = False Then
message = MsgBox("提示用户名不能为空!", vbInformation + vbOKOnly, " 警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If

If ckuid.strLenght(username) Then
message = MsgBox("提示:用户名名称过长!", vbInformation + vbOKOnly, "警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If
End Sub
suxylin 2004-10-15
  • 打赏
  • 举报
回复
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
tiandiqing 2004-10-15
  • 打赏
  • 举报
回复
'类:判断长度
Public Function strLenght(ByVal str As String) As Boolean
If Len(str) = 0 Then
strLenght = False
Exit Function
End If

If Len(str) > 16 Then
strLenght = False
Exit Function
End If

If Len(str) < 1 Then
strLenght = False
Exit Function
End If
End Function



‘我的代码(调用)

Private Sub Command1_Click()
Dim username As String
Dim ckuid As Check_Class
Dim pwd1 As String
Dim pwd2 As String
Dim message As String
Set ckuid = New Check_Class


username = Form1.username.Text
pwd1 = Form1.pwd1.Text
pwd2 = Form1.pwd2.Text


If ckuid.strLenght(username) Then
message = MsgBox("提示用户名不能为空!", vbInformation + vbOKOnly, " 警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If

If ckuid.strLenght(username) Then
message = MsgBox("提示:用户名名称过长!", vbInformation + vbOKOnly, "警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If
End Sub


//==================================
我改过来它也不执行呀!!!

错在哪里了???
Tiger_Zhao 2004-10-14
  • 打赏
  • 举报
回复
关于 2)长度是否超过了自定义的长?
你只要设置了 MaxLength 属性就根本无法输入超长的字符。
victorycyz 2004-10-14
  • 打赏
  • 举报
回复
strlenght只有一个参数,你在调用的时候,怎么跑出来两个参数了?

仔细看看我给你的例子吧,那才是真正的面向对象。
tiandiqing 2004-10-14
  • 打赏
  • 举报
回复
'类:判断长度
Public Function strLenght(ByVal str As String) As Boolean
If Len(str) = 0 Then
strLenght = False
Exit Function
End If

If Len(str) > 16 Then
strLenght = False
Exit Function
End If

If Len(str) < 1 Then
strLenght = False
Exit Function
End If
End Function



‘我的代码(调用)

Private Sub Command1_Click()
Dim username As String
Dim ckuid As Check_Class
Dim pwd1 As String
Dim pwd2 As String
Dim message As String
Set ckuid = New Check_Class


username = Form1.username.Text
pwd1 = Form1.pwd1.Text
pwd2 = Form1.pwd2.Text


If ckuid.strLenght(username, 16) Then
message = MsgBox("提示用户名不能为空!", vbInformation + vbOKOnly, " 警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If

If ckuid.strLenght(username, 16) Then
message = MsgBox("提示:用户名名称过长!", vbInformation + vbOKOnly, "警告")
Form1.username.Text = ""
Form1.username.SetFocus
Exit Sub
End If
End Sub


GGL123 2004-10-14
  • 打赏
  • 举报
回复
那你是自怎么调用的?拿点代码来让大家帮你看一下。
tiandiqing 2004-10-14
  • 打赏
  • 举报
回复
我自己写好了,但是就是不调用!
zdcwin 2004-10-14
  • 打赏
  • 举报
回复
我这里有一段类似的代码。它可以用于用户提交注册信息中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
还想懒够 2004-10-14
  • 打赏
  • 举报
回复
如果需要类,那么就在类中定义一个变量为TextBox,然后将上述代码中的Text1换成该变量,调用时先设定该变量为需要进行处理的变量。
GGL123 2004-10-14
  • 打赏
  • 举报
回复
用函数就行:
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
还想懒够 2004-10-14
  • 打赏
  • 举报
回复
不需要类吧,写几个函数就可以了啊

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))
victorycyz 2004-10-14
  • 打赏
  • 举报
回复
给你一个例子,可以设置只允许输入数字,或只允许输入字母。其它的条件你自己去扩展。

类名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

tiandiqing 2004-10-14
  • 打赏
  • 举报
回复
50分没有人拿?
难道你们编程不从面向对象考虑??

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧