0

I need to verify that the user entered a CAPITAL letter (A-Z) or a few different punctuation marks (, - ') in an input box. They can only enter one of these characters. Can anyone help me with this?

Thanks in advance!

Something like this:

strtest = InputBox("Enter a capital letter or punctuation mark:")
If strtest <> [A-Z , ' -] Then MsgBox "Invalid Entry."
3
  • what is your current code with inputbox in it? I'm asking to provide more accurate suggestion... Commented Jun 25, 2013 at 18:55
  • I edited my question to include an example of what I am looking for. Thanks! Commented Jun 25, 2013 at 18:57
  • it seems that best option is to use RegExp in your situation. There are plenty of examples here in SO. I don't have enough experience with that to help you more but you could start with this link Commented Jun 25, 2013 at 19:15

1 Answer 1

0

Like KazJaw, I suspect RegExp is probably the best bet; but I don't have enough experience with it either. So here's another approach:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked. Took more than I thought but it works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.