• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Character Prevention

How do I prevent a user from using , (comma) or " (quotes) in a text box.
0
rhariz
Asked:
rhariz
  • 4
  • 4
1 Solution
 
rharizAuthor Commented:
null
0
 
rharizAuthor Commented:
Edited text of question.
0
 
ggilmanCommented:
You can capture the text change event for the text box.


Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
   
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
        For i = 1 To Len(testText)
            char = Mid(testText, i, 1)
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
        Text1.Text = myText
    End If
End Sub
0
SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

 
ggilmanCommented:
Same as other post but now commented

Option Explicit
Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
   
    'Test to see if any invalid charachters are being used
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
       
        'Loop though all of the charachters in the string
        For i = 1 To Len(testText)
       
            'Grab one charachter at a time
            char = Mid(testText, i, 1)
           
            'If it isn't an offending charchter, keep it
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
       
        'Replace the value in the text box with our value
        Text1.Text = myText
    End If
End Sub
0
 
ggilmanCommented:
This code is a little smaller and you won't loose your cursor position.

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    Dim position As Long
    If KeyCode = 188 Or KeyCode = 222 Then
        position = Text1.SelStart
        Text1.Text = Mid(Text1.Text, 1, Text1.SelStart - 1) & Mid(Text1.Text, Text1.SelStart + 1)
        Text1.SelStart = position - 1
    End If
End Sub
0
 
khurramkcsCommented:
Here is what'll do it efficiently:

Private Sub Text1_KeyPress(KeyAscii As Integer)
  ' ASCII code of comma is 44, that of quotes is 34.
  ' But it won't be a good idea to hardcode the codes!
  If KeyAscii = Asc("""") Or KeyAscii = Asc(",") Then
    KeyAscii = 0
  End If
End Sub
0
 
ggilmanCommented:
Only problem with khurramks code, as well as my second attempt, is that if someone does a copy and then pastes to the text box, a comma or quote can be left. If you can find a way to turn paste off it won't be a problem but otherwise, you might need to go back to my first attempt. Here I added the code so you won't loose your cursor position.

Option Explicit
Private Sub Text1_Change()
    Dim myText As String
    Dim testText As String
    Dim char As String
    Dim i As Long
    Dim position As Long
     
    'Test to see if any invalid charachters are being used
    If InStr(Text1.Text, ",") Or InStr(Text1.Text, """") Then
        testText = Text1.Text
        position = Text1.SelStart
         
        'Loop though all of the charachters in the string
        For i = 1 To Len(testText)
         
            'Grab one charachter at a time
            char = Mid(testText, i, 1)
             
            'If it isn't an offending charchter, keep it
            If char <> "," And char <> """" Then
                myText = myText & char
            End If
        Next i
         
        'Replace the value in the text box with our value
        Text1.Text = myText
        Text1.SelStart = position
    End If
End Sub
0
 
rharizAuthor Commented:
Can't allow the characters to be pasted.
0
 
rharizAuthor Commented:
Works like a charm.  This is great that you caught the cursor position moving.  I noticed that in memo fields the cursor moved to the beginning of the line when attempting to insert quotation marks.
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now