Check a string for valid characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vusys
    New Member
    • Jan 2008
    • 3

    Check a string for valid characters

    Apologies for posting a question on my first post, anyhow, could someone suggest an idea to turn this into working VB6 code:

    ValidChars = "0123456789 ABC"

    If (TextBox1.Text only contains characters from ValidChars) then
    ' Do whatever '
    Else
    ' Do something else '
    EndIf
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Check using string function to find out the position of the character.

    If that is not 0 then it is a valid character so accept only those else don't accept.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      Instead of checking for Validated data inside textbox, dont allow the user to enter Invalid data.. This is called as "Input Validation" accept only required data. write this code in Keypress event of Textbox:

      [code=vb6]
      Dim ValidChars As String
      ValidChars = "0123456789 ABC"
      If KeyAscii <> 8 Then 'Allow BackSpace
      If InStr(ValidChar s, Chr(KeyAscii)) =0 Then KeyAscii=0
      End If
      [/code]

      Regards
      Veena

      Comment

      • mafaisal
        New Member
        • Sep 2007
        • 142

        #4
        Hello
        U can use Instr Function for this
        eg

        Code:
        dim ValidChars as string
        ValidChars = "0123456789ABC"
        Dim i,Flag as integer
        Flag =0
        For i = 0 to len(Textbox1.text)
         If InStr(1, ValidChars , mid(Textbox1.text,i,1)) = 0 then ' Not valid
           Flag =1
         End if 
        Next
        If Flag = 1 then 'Not Valid
          ' Do whatever '
        Else
          ' Do something else '
        EndIf
        Try

        Faisal

        Comment

        • Vusys
          New Member
          • Jan 2008
          • 3

          #5
          Thank you all for the quick responces,

          Originally posted by QVeen72
          Hi,

          Instead of checking for Validated data inside textbox, dont allow the user to enter Invalid data.. This is called as "Input Validation" accept only required data. write this code in Keypress event of Textbox:

          [code=vb6]
          Dim ValidChars As String
          ValidChars = "0123456789 ABC"
          If KeyAscii <> 8 Then 'Allow BackSpace
          If InStr(ValidChar s, Chr(KeyAscii)) =0 Then KeyAscii=0
          End If
          [/code]

          Regards
          Veena
          I should have been more clear in my first post, but I'm actually putting the text into a string and then checking it, since the textbox is used for more than one thing.

          Originally posted by mafaisal
          Hello
          U can use Instr Function for this
          eg

          Code:
          dim ValidChars as string
          ValidChars = "0123456789ABC"
          Dim i,Flag as integer
          Flag =0
          For i = 0 to len(Textbox1.text)
           If InStr(1, ValidChars , mid(Textbox1.text,i,1)) = 0 then ' Not valid
             Flag =1
           End if 
          Next
          If Flag = 1 then 'Not Valid
            ' Do whatever '
          Else
            ' Do something else '
          EndIf
          Try

          Faisal
          I tried this in my project, and an empty new one to check the code and got this both times:

          Run-time error '5':
          Invalid procedure call or argument.

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Hi,

            Change the For loop parameters:

            For i = 1 to len(Textbox1.te xt)

            Regards
            Veena

            Comment

            • Vusys
              New Member
              • Jan 2008
              • 3

              #7
              Originally posted by QVeen72
              Hi,

              Change the For loop parameters:

              For i = 1 to len(Textbox1.te xt)

              Regards
              Veena
              Aha. Thank you kindy, this works just as needed. :]

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                [CODE=vb]
                ' ...
                Flag = 0
                For i = 1 to Len(Textbox1.Te xt)
                If InStr(1, ValidChars , Mid(Textbox1.Te xt, i, 1)) = 0 Then
                Flag =1
                End If
                Next
                [/CODE]
                Just wanted to point out that as a general rule, you should immediately exit the loop once you have found the situation you're looking for. In other words, in this example you would insert Exit For after line 5.

                Comment

                Working...