+ Reply to Thread
Results 1 to 9 of 9

VBA Validate textbox value to have alphanumeric characters

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    VBA Validate textbox value to have alphanumeric characters

    Hi all!

    Please assist me in solving this problem.
    I just want the textbox to check upon exit event if the value entered is alphanumeric.
    I have a code that checks every keypress if user enters non-alphanumeric characters. But again, I also want it to make sure that user entered alphanumeric characters coz this textbox is for user password.

    Thanks!

    Here's my code:
    Private Sub Textbox2_KeyPress (ByVal KeyAscii As MSForms.ReturnInteger)
    If Textbox2.Name <> "textbox2" Then
    Select Case KeyAscii
    Case 46, 48 to 57, 65 to 122
    Case Else
    KeyAscii = 0
    Msgbox "Please enter alphanumeric characters only."
    End Select
    End If

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: VBA Validate textbox value to have alphanumeric characters

    Would something like this help you out?

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim txtVal As String
        Dim txtLength As Integer
        Dim val As String
        
        txtVal = TextBox1.Value
        txtLength = Len(txtVal)
        
        For i = 1 To txtLength
            val = Mid(txtVal, i, 1)
            Select Case Asc(UCase(val))
                Case 48 To 57, 65 To 90
                Case Else
                    MsgBox "NO GOOD BRO!"
                    Cancel = True
                    TextBox1.SetFocus
                    TextBox1.SelStart = 0
                    TextBox1.SelLength = txtLength
                    Exit Sub
            End Select
        Next i
    End Sub

  3. #3
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA Validate textbox value to have alphanumeric characters

    Hi BigBas!
    Thanks for replying
    I tried your code but the textbox accepts all type of entries upon exit, may it be letters only, numbers only, or aplhanumeric.

    Any more insight?
    Thanks!

    Any more insight?
    Thanks!

  4. #4
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: VBA Validate textbox value to have alphanumeric characters

    Perhaps I misunderstood your request. You are correct, the code allows letters, numbers, or both. Are you asking to REQUIRE that the user enter at least one letter and one number?

  5. #5
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA Validate textbox value to have alphanumeric characters

    Quote Originally Posted by BigBas View Post
    Perhaps I misunderstood your request. You are correct, the code allows letters, numbers, or both. Are you asking to REQUIRE that the user enter at least one letter and one number?
    Yes, BigBas! I'm sorry if I confused you. This textbox is where user needs to enter their new password. And I believe, for better security, it's better for a password to be alphanumeric, right?

    Thanks again in advance!

  6. #6
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: VBA Validate textbox value to have alphanumeric characters

    That sounds right. Earlier, I though you were trying to block the user from entering NON alpha-numeric data (special characters such as: ! @ # $ etc).

    The below code requires at least 1 letter and 1 number. You can further strengthen the password by requiring a minimum length, or any other requirements.

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Dim txtVal As String
        Dim txtLength As Integer
        Dim val As String
        Dim nums As Integer
        Dim letters As Integer
        
        txtVal = TextBox1.Value
        txtLength = Len(txtVal)
        
        nums = 0
        letters = 0
        For i = 1 To txtLength
            val = Mid(txtVal, i, 1)
            Select Case Asc(UCase(val))
                Case 48 To 57
                    nums = nums + 1
                Case 65 To 90
                    letters = letters + 1
                Case Else
            End Select
        Next i
        
        If letters = 0 Or nums = 0 Then
            MsgBox "NO GOOD BRO!"
            Cancel = True
            TextBox1.SetFocus
            TextBox1.SelStart = 0
            TextBox1.SelLength = txtLength
        End If
    End Sub

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: VBA Validate textbox value to have alphanumeric characters

    Hi,

    I always thought lower case, upper case along with numbers in a password made it stronger. See:
    http://netforbeginners.about.com/od/...g-password.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  8. #8
    Registered User
    Join Date
    01-15-2013
    Location
    Dubai, UAE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: VBA Validate textbox value to have alphanumeric characters

    Thank you so much, BigBas!!!
    That worked perfectly! Also, I limit the password length to 10 characters only, as you recommended.

    Hi MarvinP,
    I'll also look into your link to find more insights. For now, I'm tagging this post as SOLVED!

    Thanks for this forum as always!

  9. #9
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: VBA Validate textbox value to have alphanumeric characters

    Glad to help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1