有谁可以给我个简单的类,来检查用户文本框: 1)是否为空
                                          2)长度是否超过了自定义的长?
                                          3)只可以输入汉字
                                          4)只可以输入字母
                                          5)只可以输入数字
                                          6)当文本框既有汉字,又有字母时候他的长度是多少?
好,就这么多吧,我自己写了一个,但就是不调用,哪为高人帮我指点一个思路??

解决方案 »

  1.   

    给你一个例子,可以设置只允许输入数字,或只允许输入字母。其它的条件你自己去扩展。类名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,text2dim 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
      

  2.   

    不需要类吧,写几个函数就可以了啊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 Sub4、只可以输入字母
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       
        If (KeyAscii < 65 Or KeyAscii > 122) And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        
    End Sub4、只可以输入数字
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        
    End Sub5、混合长度
    LenB(StrConv(Text1.text, vbFromUnicode))
      

  3.   

    用函数就行:
    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
      

  4.   

    如果需要类,那么就在类中定义一个变量为TextBox,然后将上述代码中的Text1换成该变量,调用时先设定该变量为需要进行处理的变量。
      

  5.   

    我这里有一段类似的代码。它可以用于用户提交注册信息中EMAIL的合法性检测。
    Private Function CheckData() As Boolean
     If Len(Trim(EmailInputBox.Text)) < 7 Or Len(Trim(EmailInputBox.Text)) > 40 Then
    ‘对于一个合法的EMAIL地址,它必定是此种格式:[email protected],长度最少为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
    基本上能实现你的要求,具体代码你自己改改吧。如果还有问题,联系我:[email protected]
      

  6.   

    '类:判断长度
    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
      

  7.   

    strlenght只有一个参数,你在调用的时候,怎么跑出来两个参数了?仔细看看我给你的例子吧,那才是真正的面向对象。
      

  8.   

    关于 2)长度是否超过了自定义的长?
    你只要设置了 MaxLength 属性就根本无法输入超长的字符。
      

  9.   

    楼上的说得好!
    设置他的MaxLength
      

  10.   

    '类:判断长度
    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
    //==================================
    我改过来它也不执行呀!!!错在哪里了???
      

  11.   

    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
      

  12.   

    //==================================
    //我改过来它也不执行呀!!!//错在哪里了???应该这样调用,另外你的代码还有问题,请自查。不过这样就可以调用它了。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
      

  13.   


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

  14.   

    victorycyz(中海) 我想问一下正常应怎么写,我怎么调试他都显示用户名不能为空!
      

  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
      

  16.   

    上面的 If a = False Then 应为if a=1 then 特此更正。