Help with validation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dwd3773
    New Member
    • Jul 2007
    • 3

    Help with validation problem

    I'm trying to get a validation of a text box to where if the information is not letters then it will display a error message. I know how to get the error message, but I dont dont know how to do a validation with letters. I know how to do it with number but not letters. Please help. Thanks.
  • fplesco
    New Member
    • Jul 2007
    • 82

    #2
    Originally posted by dwd3773
    I'm trying to get a validation of a text box to where if the information is not letters then it will display a error message. I know how to get the error message, but I dont dont know how to do a validation with letters. I know how to do it with number but not letters. Please help. Thanks.
    Hi dwd3773 -

    You may try these codes below

    Code:
    Const strAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Private Sub cmdValidate_Click()
        If InvalidTextboxInput = True Then
            MsgBox "Textbox contains invalid character.", vbExclamation
        Else
            MsgBox "Valid textbox content.", vbInformation
        End If
    End Sub
    
    Private Function InvalidTextboxInput() As Boolean
        For i = 1 To Len(TextBox1.Text)
            If InStr(strAlphabet, UCase(Mid(TextBox1.Text, i, 1))) < 1 Then
                InvalidTextboxInput = True
                Exit For
            End If
        Next i
    End Function
    Godspeed!

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by dwd3773
      I'm trying to get a validation of a text box ...
      Quick question - what version of VB are you using?

      Oh, and another question - have you considered using a masked edit control rather than a textbox? The masked edit control gives you more control over the format of the input.

      Comment

      • dwd3773
        New Member
        • Jul 2007
        • 3

        #4
        No, I haven't thought of using one of those. I have strict guideline from my customer as to what they want, but I've never really validated letters only numbers. I 'm using Microsoft Visual Studio 2005.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by dwd3773
          No, I haven't thought of using one of those. I have strict guideline from my customer as to what they want, but I've never really validated letters only numbers. I 'm using Microsoft Visual Studio 2005.
          Fair enough.

          You might as well go with the routine that fplesco provided. It looks as though it will work. One small change I would recommend, though. That is to have it accept the textbox (or just the text) as a parameter, so it's more widely applicable. Seems as though it would make a useful public function to add to your tool kit.

          Comment

          • fplesco
            New Member
            • Jul 2007
            • 82

            #6
            Originally posted by Killer42
            Fair enough.

            You might as well go with the routine that fplesco provided. It looks as though it will work. One small change I would recommend, though. That is to have it accept the textbox (or just the text) as a parameter, so it's more widely applicable. Seems as though it would make a useful public function to add to your tool kit.

            [That is to have it accept the textbox (or just the text) as a parameter, so it's more widely applicable]

            Good point Killer42. Thanks.

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by fplesco
              Good point Killer42. Thanks.
              I think it's a good general technique to try and write all one's routines so they're reusable. Though of course there are other considerations, such as performance.

              (And of course, being so disorganised, I can never find them again later, anyway. But my intentions are good.)

              Comment

              Working...