How to stop a user inputting special charachters in a comments box on a user form?
So no |/\+-+}{# etc
How to stop a user inputting special charachters in a comments box on a user form?
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.Originally Posted by khalid79m
Thanks,
Mark
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]
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
THANKS excellent code