Consulting

Results 1 to 5 of 5

Thread: Solved: Restrict Special Charachters

  1. #1

    Solved: Restrict Special Charachters

    How to stop a user inputting special charachters in a comments box on a user form?

    So no |/\+-+}{# etc

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by khalid79m
    ...So no |/\+-+}{# etc
    How about telling what is actually to be allowed and what is not. If we are only restricting certain characters, the answer can be different than for instance, if we are only allowing letters and numbers.

    Thanks,

    Mark

  3. #3
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Assuming that th comments box is called textbox1 this will work for you, just add the characters you want removed into LkFr
    [VBA]
    Private Sub TextBox1_Change()
    Dim LkFr As String, I As Long
    LkFr = "|/\+-+}{#"
    If InStr(1, LkFr, Right(TextBox1.Text, 1)) Then
    If Len(TextBox1.Text) > 1 Then
    TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
    Else
    TextBox1.Text = ""
    End If
    End If
    End Sub
    [/VBA]

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    In your Submit button code put something like this:

    [vba]
    If Not UCase$(TextBox1.Value) Like "[A-Z]" Then
    MsgBox "Text box must contain alphanumeric characters only"
    Exit Sub
    End If
    [/vba]

    Sorry, it should actually be a loop:

    [VBA]
    Dim i As Long

    For i = 1 to Len(TextBox1.Value)
    If Mid$(TextBox1.Value,i,1) Like "[!A-Z]" Then
    MsgBox "Text box must contain alphanumeric characters only"
    Exit Sub
    End If
    Next i
    [/VBA]



    Assuming it is a text box. Even better, highlight the field in red and move the focus to the text box so the user can correct it.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  5. #5
    THANKS excellent code

Posting Permissions

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