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
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
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
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
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
'类:判断长度
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
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
'类:判断长度
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
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
我这里有一段类似的代码。它可以用于用户提交注册信息中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
用函数就行:
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
类名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