View Full Version : Solved: Restrict Special Charachters
khalid79m
03-22-2011, 07:56 AM
How to stop a user inputting special charachters in a comments box on a user form?
 
So no |/\+-+}{# etc
...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
Tommy
03-22-2011, 08:15 AM
Assuming that th comments box is called textbox1 this will work for you, just add the characters you want removed into LkFr
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
JP2112
03-23-2011, 06:04 AM
In your Submit button code put something like this:
 
 
If Not UCase$(TextBox1.Value) Like "[A-Z]" Then
MsgBox "Text box must contain alphanumeric characters only"
Exit Sub
End If
 
Sorry, it should actually be a loop:
 
 
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
 
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.
khalid79m
03-28-2011, 01:32 PM
THANKS excellent code :)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.