0

我需要验证用户是否在输入框中输入了大写字母 (AZ) 或几个不同的标点符号 (, - ')。他们只能输入这些字符之一。谁能帮我这个?

提前致谢!

像这样的东西:

strtest = InputBox("Enter a capital letter or punctuation mark:")
If strtest <> [A-Z , ' -] Then MsgBox "Invalid Entry."
4

1 回答 1

0

和 KazJaw 一样,我怀疑 RegExp 可能是最好的选择。但我也没有足够的经验。所以这是另一种方法:

Function TestEntry(sTest As String) As Boolean

    Dim bTemp As Boolean
    Dim sOKChars As String
    Dim x As Long

    bTemp = False ' by default
    sOKChars = "!@#$%^&*()_+" ' add/remove chars as appropriate

    If Len(sTest) > 1 Then
        MsgBox "Doggone it, I said ONE character only. Try again."
        ' I'd remove the msgbox and test the Len before passing the 
        ' string to this function, actually
    End If

    If 64 < Asc(sTest) And Asc(sTest) < 91 Then
        bTemp = True
    End If

    If Not bTemp Then
        For x = 1 To Len(sOKChars)
            If Mid$(sOKChars, x, 1) = sTest Then
                bTemp = True
                Exit For
            End If
        Next
    End If

    TestEntry = bTemp

End Function

Sub TestTestEntry()
    If TestEntry(",") Then
        MsgBox "Ok"
    Else
        MsgBox "NOT ok"
    End If

End Sub
于 2013-06-25T20:41:15.887 回答