[RESOLVED] String Manipulation Help
how could i make pure text only when i type on a textbox? numbers must not be accept... but when i type something like this asdf123 its accept... how could i make it pure text only?
Code:
if isnumeric(text1.text) = true then
msgbox "No Numbers Allowed"
end if
Re: String Manipulation Help
You can trap keys in the textbox KeyPress event and simply ignore all numeric keys in the range of Asc("0") to Asc("9").
Re: String Manipulation Help
Something like this: ...:wave:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then '~~~> If it is not a number (0 - 9) Ascii value = 48 - 57
If KeyAscii <> 8 Then '~~~> Allow Backspace
KeyAscii = 0 '~~~> Make it an invalid entry
End If
End If
End Sub
Re: String Manipulation Help
Code:
Private Sub Command1_Click()
If IsNumeric(Text1) = True Then
MsgBox "No Numeric Allowed", vbCritical, "warning"
MsgBox "Must Be Text only",vbCritical, "warning"
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
End Sub
it allows this asdf123 when i inputted what's wrong with my code?
also allows this asdf123asdf i want to happen is only text only must be entered
Re: String Manipulation Help
You put it in the wrong event. As akhileshbc showed, it goes in the KeyPress event of the textbox.
I can't tell if you want to disallow numbers or only allow letters. If the latter:
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 65 To 90, 97 To 122 ' A-Z, a-z
Case Else: KeyAscii = 0
End Select
End Sub
Re: String Manipulation Help
My Version
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
Re: String Manipulation Help
That doesn't prevent pasting from the clipboard, contrary to what the comments say. You have to handle pasting from the Change event.
Re: String Manipulation Help
Using LIKE operator...
Code:
Private Sub Command1_Click()
If Not Text1.Text Like "*[!0-9]*" Then
MsgBox "Valid.. Contains only digits (0 to 9)"
Else
MsgBox "Contains something else..."
End If
End Sub
More info: http://www.devx.com/vb2themax/Tip/18578 ...:wave:
Re: String Manipulation Help
Here is the code for disabling paste using right mouse click... I am sure the user can take care of insert like the way I took care of Ctrl + V
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
'~~> Disable paste using right click
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Clipboard.Clear
End If
End Sub
Re: String Manipulation Help
I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?
I tried all the codes and it works
Re: String Manipulation Help
Quote:
Originally Posted by
dbasenoob
I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?
Could you make it clear..??? It is confusing us.... :confused:
Re: String Manipulation Help
Quote:
Originally Posted by
koolsid
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'~~> Ensures numbers are not keyed it
'~~> Also ensure the user is not able to paste number
If KeyAscii > 47 And KeyAscii < 58 Or _
KeyAscii = 22 Then KeyAscii = 0
End Sub
Pasting can also be done via Shift+Ins, which does not fire a KeyPress event.
Quote:
'~~> Disable paste using right click
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Clipboard.Clear
End If
End Sub
[/CODE]
It is a bad practice to unexpectedly clear the clipboard. For example, most of us rightfully think it's cheesy and lame that opening VB6 clears the clipboard.
Also, the context menu (and by extension Paste) can be opened using the Context Menu key on the keyboard, which doesn't fire a KeyPress event.
Shift+Ins and the Context Menu keys do fire KeyDown() events, though. But even still, unexpectedly clearing the clipboard is a bad practice.
1 Attachment(s)
Re: String Manipulation Help
Quote:
Originally Posted by
dbasenoob
I tried to paste number it works but, when i used to type not working... How to prevent the user to paste it?
I tried all the codes and it works
Cannot get better than this....
Eliis: didn't see your post... gimme a moment....
I have updated the attachment... Now try it...
Re: String Manipulation Help
Sweet! That's full of win.
(You should pull out the right+click code since it doesn't do anything except unexpectedly clear the clipboard.)
Re: String Manipulation Help
Although you should technically be allowed to paste valid text. I still maintain that clearing out invalid characters in the Change() event is the better way to handle pasting.
Does anyone know how Microsoft handles it? Are there any limited-character textboxes in Windows or Office we can play with to see how they handle pasting invalid characters?
EDIT: Microsoft allows invalid characters. For example, you can type letters into any numeric textbox under Tools=>Options in both Excel and Word, and if you right-click the desktop and go to the Screen Saver tab, you can type letters in the "Wait ... minutes" textbox.
Re: String Manipulation Help
Quote:
Originally Posted by
Ellis Dee
Sweet! That's full of win.
(You should pull out the right+click code since it doesn't do anything except unexpectedly clear the clipboard.)
Already updated the attachment above ;)