我现在要写一个函数StrCheck,功能是对输入的字符串进行Check,看是否合法,如果不合法则返回非法字符串.
要求输入的字符串中只能包含半角数字字母以及其他一些半角字符,不允许输入"*","&"等半角字符以及所有全角字符和汉字
输入参数:需要Check的字符串
输出参数:1.进行Check的字符串是否合法true,false 
         2.如果不合法则返回非法字符串
返回的非法字符串格式如下:
例如:输入*123测试9aa则返回的非法字符串为:* 测 试.也就是说将非法字符挑出来用半角空格隔开.
问题是如果是汉字或者全角文字的时候怎么做?

解决方案 »

  1.   

    Private Function StrCheck(s As String) As String
        Dim ch As String, i As Long, n As Long
        For i = 1 To Len(s)
            ch = Mid(s, i, 1)
            n = Asc(ch)
            If n < 32 Or n > 126 Or n = 42 Or n = 38 Then
                StrCheck = StrCheck & ch & " "
            End If
        Next
        StrCheck = Trim(StrCheck)
    End Function
      

  2.   

    做一个合法表,然后instr判断即可。
      

  3.   

    Dim valid As Stringvalid = "1234567890abcdefg,."Private Function isvalidstring( ByVal s As String ) As Boolean
    Dim i As long
    Dim l As long

    l = Len( s )
    For i = 1 To l
    If InStr( 1, valid, Mid(s,i,1) ) = 0 Then
    isvalidstring = False
    endif
    Next i

    isvalidstring = True
    End Function
      

  4.   

    试一下这段代码:
    Const InvaString = "*&" '所有无效的西文字符
    Dim strNonlicet$        '返回字符串Function StringChk(strChk$) As Boolean
    '处理过程
    '含非法字符返回 True ,变量 strNonlicet 中包含分析结果
    '无非法字符返回 False,变量 strNonlicet = ""
        Dim i&, iFlag&, cTemp$, bResult As Boolean
        strNonlicet = "": iFlag = 0: bResult = False
        For i = 1 To Len(strChk)
            cTemp = Mid$(strChk, i, 1)
            If (Asc(cTemp) < 32) Then
                iFlag = iFlag Or 2
            Else
                If (InStr(1, InvaString, cTemp) > 0) Then
                    iFlag = iFlag Or 2
                Else
                    iFlag = 0
                End If
            End If
            If (iFlag > 1) Then
                bResult = True
                If (iFlag = 2) Then
                    strNonlicet = strNonlicet & " " & cTemp
                    iFlag = iFlag Or 1
                Else
                    strNonlicet = strNonlicet & cTemp
                End If
            End If
        Next
        strNonlicet = LTrim$(strNonlicet)
        StringChk = bResult
    End FunctionSub Test()
    '测试过程
        If (StringChk("123*&*&46汉字+++++中文")) Then
            Debug.Print strNonlicet
        Else
            Debug.Print "<Licit!>"
        End If
        If (StringChk("全中文字符串")) Then
            Debug.Print strNonlicet
        Else
            Debug.Print "<Licit!>"
        End If
        If (StringChk("ABCD123456")) Then
            Debug.Print strNonlicet
        Else
            Debug.Print "<Licit!>"
        End If
    End Sub
      

  5.   

    要求输入的字符串中只能包含半角数字字母以及其他一些半角字符
    ------
    这就简单了,要求包含的只是一小部分半角字符,以及数字字母.原理就是用MID函数将目标串的每个字符提取出来逐个与要求的字符相比较,看是否满足要求.数字字母的ASCII码是具有连续数值的区间.用 IF ASC(MID(目标串,I,1))>ASC("A") AND ASC(MID(目标串,I,1))<ASC("Z") 就可以确定是否在A-Z范围里,再对0-9,a-z判断.之后,对那些少数的半角字符,可以用下面直观简洁的方式:
    SELECT CASE ASC(MID(目标串,I,1))
      CASE ASC("$"),ASC("_"),ASC("其它允许的字符")
           标记为合法
      CASE ELSE
           标记为不合法
    END SELECT
      

  6.   

    更正一下,应该是 IF ASC(MID(目标串,I,1))>=ASC("A") AND ASC(MID(目标串,I,1)) =<ASC("Z")