Results 1 to 7 of 7

Thread: Textbox Validation Function - Check it out

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Thumbs up Textbox Validation Function - Check it out

    I have been noticing that a few persons have posted questions on how they can validate text box contains. I had to design a funcionfor a cusomer that would only allow the user to enter data in the following formats:

    E05-09812A
    WS0/3456SA

    So I came up with the following function. This function allows the programmer the ability to control what type of alphanumeric character is entered into a text box at a particular postion in the text box.

    VB Code:
    1. Private Function ValidateKey(TextBoxName As TextBox, Keyascii As Integer) As Integer
    2. Dim strTagFilter As String
    3. Dim intPosition As Integer
    4.  
    5. If Keyascii = 8 Then
    6.     Exit Function
    7. Else
    8.     If TextBoxName.Tag & "" = "" Then
    9.         Exit Function
    10.     Else        intPosition = Len(TextBoxName.Text) + 1 'Sets up the Tag pointer
    11.         strTagFilter = Mid(TextBoxName.Tag, intPosition, 1) 'Gets the Validator Code
    12.        
    13.         If intPosition > Len(TextBoxName.Tag) Then 'Only allows the same number of characters
    14.                                           'that are contained in the Tag Property
    15.            
    16.             Keyascii = 0  'If so doesn't allow anymore characters
    17.         Else
    18.             TextBoxName.SelStart = intPosition 'Positions alphanumeric
    19.                                                 'character after previous
    20.                                                 'alphanumeric character
    21.             Select Case strTagFilter
    22.                 Case "A", "a" 'Only allows Letters
    23.                     If (Keyascii >= 65 And Keyascii <= 90) Or _
    24.                             (Keyascii >= 97 And Keyascii <= 122) Then
    25.                        
    26.                             Keyascii = Keyascii
    27.                         If strTagFilter = "A" Then 'Capitalize the letter if "A"
    28.                            If (Keyascii >= 97 And Keyascii <= 122) Then
    29.                                 Keyascii = Keyascii - 32 ' Capitalize the first charater
    30.                            End If
    31.                         End If
    32.                     Else
    33.                         Keyascii = 0 'invalid character
    34.                     End If
    35.                    
    36.                 Case "0"    'Only allows numbers
    37.                     If (Keyascii < 48 Or Keyascii > 57) Then
    38.                         Keyascii = 0 'invalid character
    39.                     End If
    40.                 Case Else
    41.                     If Keyascii <> Asc(strTagFilter) Then 'Only allows the same alphanumeric
    42.                                                           'Character (i.e. /-_=+&*()#$@!{}|\)
    43.                        Keyascii = 0 'invalid character
    44.                     End If
    45.             End Select
    46.         End If
    47.     End If
    48. End If
    49. End Function

    ... and you call it like this ...
    VB Code:
    1. Private Sub Text1_KeyPress(Keyascii As Integer)
    2.     Call ValidateKey(Me.Text1, Keyascii)
    3. End Sub

    So to allow the following number to be entered into a text box US35-39234B then enter th following into that textboxes Tag Property: AA00-00000A
    Last edited by Mark Gambo; May 23rd, 2005 at 05:50 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Textbox Validation Function - Check it out

    You should've posted it in the Code Bank instead.

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Textbox Validation Function - Check it out

    Quote Originally Posted by RhinoBull
    You should've posted it in the Code Bank instead.
    Yes I probably should have, but I wanted to make sure that the code was solid and that I didn't over look anything before it was put into the Codebank.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Textbox Validation Function - Check it out

    If you're already passing the TextBox to the function why do you also need to pass the text and the tag? Can't the function read those properties by itself?

  5. #5
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Textbox Validation Function - Check it out

    you could have simply used this:

    Code:
    Private Sub Command1_Click()
    'E05-09812A
    'WS0/3456SA
    
    If UCase$(Text1.Text) Like "[A-Z]##-#####[A-Z]" _
        Or UCase$(Text1.Text) Like "[A-Z][A-Z]#/####[A-Z][A-Z]" Then
        MsgBox "okay"
    End If
    End Sub

  6. #6

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Textbox Validation Function - Check it out

    Quote Originally Posted by Joacim Andersson
    If you're already passing the TextBox to the function why do you also need to pass the text and the tag? Can't the function read those properties by itself?
    Joacim,
    Thanks for your analysis, I have made the changes to my original post (they are indicted in red).
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Thumbs up Re: Textbox Validation Function - Check it out

    Quote Originally Posted by ZeBula8
    you could have simply used this:

    Code:
    Private Sub Command1_Click()
    'E05-09812A
    'WS0/3456SA
    
    If UCase$(Text1.Text) Like "[A-Z]##-#####[A-Z]" _
        Or UCase$(Text1.Text) Like "[A-Z][A-Z]#/####[A-Z][A-Z]" Then
        MsgBox "okay"
    End If
    End Sub
    ZeBula8,

    Thanks for your post, but the purpose of this function was so that I could put it into a module make it Pubic so that I could use it through out my app. Your solution appears to work but it will allow both formated numbers to be entered into a textbox but I could not with out re-writting your code exclude one of them while allowing the other. My function gives me more flexibiity in that I can use it over and over without changing anythig but the Tag property of the textbox. So that I can go from: C98-1234 to 09-234322 without changing my code or writting separate code for each text box (less code = smaller app). Anyway I appreciate your opinion.

    Thanks!!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width